Running Python code
Overview
There are two ways to run Python, and you’ll use both all the time: interactively, one line at a time, to try things out; and as a script, a saved file you can run again, share and build on.
The interactive shell (REPL)
Open a terminal and type python (or python3 on macOS/Linux) with nothing after it:
$ python
Python 3.15.2 (main, Nov 2026) on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>The >>> prompt means Python is waiting for you. Type an expression and press Enter:
>>> 7 * 6
42
>>> "ha" * 3
'hahaha'
>>> len("Python")
6This is the REPL — Read, Evaluate, Print, Loop. It reads what you type, evaluates it, prints the result, and waits for more. It’s perfect for:
- checking how something works (“what does
round(2.5)give?”), - trying a piece of code before putting it in a program,
- using Python as a powerful calculator.
Since Python 3.13 the REPL has colours, multi-line editing and history (press ↑ to bring back previous lines). Type help(len) for documentation on anything. To leave, type exit (or press Ctrl+D on macOS/Linux, Ctrl+Z then Enter on Windows).
Anything you type in the REPL is lost when you close it. For anything you want to keep, use a file.
Scripts
A script is a text file with the .py extension:
# greet.py
name = "Asha"
print("Hello,", name)
print("Your name has", len(name), "letters.")Run it from the terminal, in the file’s folder:
python greet.pyHello, Asha
Your name has 4 letters.(or use your editor’s ▶ Run button, which does the same thing).
The big difference: only print() shows output
In the REPL, typing len(name) shows 4 automatically. In a script, a line like len(name) calculates 4 and then throws it away — nothing appears. A script only shows what you explicitly print(). That’s the most common surprise when moving from the REPL to files, and it’s what the exercise is about.
What happens when you run a file
- Python reads your whole file and compiles it to bytecode — a compact set of instructions. If there’s a syntax error (a missing bracket, a misspelled keyword), it stops here, before running anything.
- The interpreter runs the bytecode, from the top of the file to the bottom.
- If something goes wrong while running (dividing by zero, using a name that doesn’t exist), Python stops at that line and shows an error message. Lines before it have already run.
You never have to compile Python yourself — python file.py does everything.
Comments
Lines starting with # are comments. Python ignores them; they’re notes for humans:
# Calculate the price including 20% tax
price = 50
total = price * 1.2 # comments can also go at the end of a lineGood comments explain why, not what — the code already says what.
A workflow that works
- Experiment in the REPL until a line does what you want.
- Put it in your script.
- Run the script often — after every few lines, not after a hundred.
- When something breaks, read the error (next lesson) and test the suspicious bit in the REPL.
Habit: keep a terminal with the REPL open next to your editor. Whenever you wonder “what would happen if…?”, try it.
From the REPL to a script
First, open the Python REPL and try these expressions one at a time: 7 * 6, 2 ** 10, "ha" * 3 and len("Python"). Notice that the REPL shows each result automatically. Then put the same four expressions in a file experiments.py and run it — nothing appears! Fix the file so it prints each result on its own line, labelled, like 7 * 6 = 42.
# experiments.py — these show results in the REPL, but not in a script
7 * 6
2 ** 10
"ha" * 3
len("Python")# experiments.py — a script only shows what you print()
print("7 * 6 =", 7 * 6)
print("2 ** 10 =", 2 ** 10)
print('"ha" * 3 =', "ha" * 3)
print('len("Python") =', len("Python"))Summary
- The REPL (
pythonwith no file) runs one line at a time and shows each result immediately — ideal for experiments. - A script is a
.pyfile run withpython file.py; it only shows what youprint(). - Python compiles your file to bytecode and runs it with the interpreter — there is no separate build step.
- Syntax errors stop the program before it runs; other errors appear when the faulty line is reached.
- Comments start with
#and are ignored by Python — use them to explain why.