Running Python code

Topic 3 of 4 in Getting started6 min readBeginnerFree course

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.

Two ways to run Python. Interactive (REPL): type python, then at the >>> prompt 2 + 3 shows 5, name = “Asha” stores a value, name.upper() shows ‘ASHA’, and exit() leaves — you try ideas instantly and results print automatically. Script file: greet.py contains name = “Asha” and print(name.upper()); running python greet.py prints ASHA — you can save, rerun and share it, and only print() shows output.

The interactive shell (REPL)

Open a terminal and type python (or python3 on macOS/Linux) with nothing after it:

Text
$ 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:

Python
>>> 7 * 6
42
>>> "ha" * 3
'hahaha'
>>> len("Python")
6

This 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:

Python
# greet.py
name = "Asha"
print("Hello,", name)
print("Your name has", len(name), "letters.")

Run it from the terminal, in the file’s folder:

Terminal
python greet.py
Text
Hello, 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

What happens when you run a Python program: your source code in hello.py goes to the compiler, which checks the syntax and makes bytecode; the interpreter runs the bytecode step by step; and the result is output such as text or files. Running python hello.py prints Hello, world!. Python is interpreted: there is no separate build step. Syntax errors are reported before anything runs; other errors appear when the line that causes them is reached.

  1. 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.
  2. The interpreter runs the bytecode, from the top of the file to the bottom.
  3. 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:

Python
# Calculate the price including 20% tax
price = 50
total = price * 1.2   # comments can also go at the end of a line

Good comments explain why, not what — the code already says what.

A workflow that works

  1. Experiment in the REPL until a line does what you want.
  2. Put it in your script.
  3. Run the script often — after every few lines, not after a hundred.
  4. 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.

Exercise

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.

Python
# experiments.py — these show results in the REPL, but not in a script
7 * 6
2 ** 10
"ha" * 3
len("Python")

Summary

  • The REPL (python with no file) runs one line at a time and shows each result immediately — ideal for experiments.
  • A script is a .py file run with python file.py; it only shows what you print().
  • 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.