Mr. Darto runs a small coffee stall. Every night he adds up the day's sales on a calculator: coffee with milk times the number of cups, sweet tea times the number of cups, then the total. If he presses one wrong key, the total is wrong and he starts again. A computer can do the same sum for him each day. To do that, you write the instructions in a programming language (a language for giving instructions to a computer), such as Python.
You write the instructions in a file whose name ends in .py. Then a program called the Python interpreter runs them one at a time, from top to bottom. The interpreter works the way Mr. Darto reads his sales notes. Before he starts adding up, he reads quickly through all the notes. If he can't read one line, he does nothing at all. If all of it is clear, he does the work line by line. In this picture, the notes are the .py file, each line is one instruction, and Mr. Darto is the interpreter.
Your first Python program
We'll use one example that keeps growing through this track: a program for the till (the cash register) at Mr. Darto's Coffee Stall. The menu is coffee with milk at $15, sweet tea at $5 and toast at $12. Here is the first program, in the file stall.py:
print("Mr. Darto's Coffee Stall")
print("Coffee with milk", 15)
print(15 * 3) # 3 cups
Run it in the terminal (the window where you type commands) with python3 stall.py. Here python3 is the interpreter, and stall.py is the file it runs. In this example, $ marks the line you type (do not type the $ itself). The lines below it are the result:
$ python3 stall.py
Mr. Darto's Coffee Stall
Coffee with milk 15
45
- Text after
#is a comment: a note for people. Python does not run it. print(...)writes to the terminal, then moves to a new line. Text goes between quotes.print("Coffee with milk", 15)writes two values at once: a text and a number. A comma insideprintbecomes one space in the output.15 * 3is calculated first (the star means times), and then its result,45, is written. Numbers have no quotes."15"in quotes is text, not a number.- One line, one instruction. Python needs no semicolons or curly braces, unlike some other languages.
On your own computer, install the latest Python from python.org (3.15 at the moment). Check it with python3 --version. It shows a line like Python 3.15.2 (your numbers may be different). On Windows the command is usually python or py, not python3. In the exercises here, the Run Test button runs your code in the browser, with a Python simulator. So you don't need to install Python yet.
How you install Python depends on your operating system:
- Windows: install the Python install manager from python.org/downloads or the Microsoft Store. It replaces the old installer (the setup program), which will not be made for Python 3.16 and later. The command is
pythonorpy. - macOS: the installer from python.org, or
brew install pythonif you use Homebrew. The command ispython3. Ignore Apple's own/usr/bin/python3, because it is still version 3.9. - Linux:
python3is usually already installed by your distribution (the kind of Linux you use), but it is often older than the latest version. - Any system: the command
uv python install, from the uv tool (a helper program for Python), installs the Python version you choose. It does not touch any other Python on your computer.
Interpreted: run right away
Python is called an interpreted language: the interpreter runs your code directly. A compiled language such as C or Go has an extra step, called compile. In that step, the code is first turned into a separate program file. That program file is what runs. Python has no compile step that you run yourself: python3 stall.py runs the .py file directly. (Inside, Python still turns your code into bytecode, a form the interpreter can run. This happens on its own, and the result is sometimes saved in a __pycache__ folder.)
Like Mr. Darto with his notes, Python first reads the whole file to check how it is written (its syntax). After that, it runs the instructions one by one from the top. So one syntax mistake on line 3 means that even line 1 does not run.
Scripts and the REPL
There are two ways to run Python:
- Script: you write the code in a
.pyfile and run it withpython3 stall.py. This is how you make real programs. - REPL: type only
python3in the terminal and the sign>>>appears. Each line you type runs right away. REPL stands for Read-Eval-Print Loop: read it, run it, show the result, repeat.
An example in the REPL:
$ python3
>>> 15 * 3
45
>>> "Coffee with milk"
'Coffee with milk'
>>> print("Coffee with milk")
Coffee with milk
>>> exit()
- In the REPL, the result of each line you type is shown right away. Text is shown with quotes,
'Coffee with milk', because the REPL shows text the way you write it in code. In Python, single quotes'and double quotes"mean the same. printwrites only the text itself, without quotes.exit()closes the REPL.
Now guess: the same line 15 * 3 is written in the file stall.py, without print, and the file is run. Does the terminal show 45, or nothing?
Nothing. A script shows only what you print. The line 15 * 3 is still calculated, but the result is thrown away right after. To see the result, write print(15 * 3).
Spaces at the start of a line mean something
Python uses indentation, the spaces at the start of a line, to group several lines into one block. You will use it later, in the If, Elif, Else lesson. Until then, each line must start at the far left, with no space before it. One space in the wrong place is enough to cause an error. In this example, line 2 starts with one space:
print("Mr. Darto's Coffee Stall")
print("Coffee with milk", 15)
print(15 * 3) # 3 cups
The result when you run it:
$ python3 stall.py
File "/home/darto/stall.py", line 2
print("Coffee with milk", 15)
IndentationError: unexpected indent
line 2points to the line with the mistake. The last line names the kind of error: unexpected indent, which means an indentation that should not be there.- Even line 1 does not run, because Python checks the whole file before it runs any line.
- The REPL is different: the lines you typed before have already run. Only the wrong line is refused.
Writing style: PEP 8
PEP 8 is the official style guide for Python code (PEP stands for Python Enhancement Proposal). It lists habits that make code easy for anyone to read:
- Indent with 4 spaces.
- Variable and function names (you will learn both later) are lowercase with underscores, called snake_case:
coffee_price, notCoffeePrice. - A line is at most 79 characters long.
🎯 What Python is used for
- Web backends (the part of a website that runs on the server): with tools such as Django, Flask and FastAPI.
- Data: pandas and NumPy for working with tables and numbers.
- AI and machine learning (computers learning from data): PyTorch and scikit-learn.
- Automation: small scripts that tidy files, send reports or collect data from the web.
⚠️ Why does your first program fail right away?
- Python 2 tutorials. Python 2 is an old version that was retired in 2020. In Python 2,
print "Hello"is written without parentheses. Python 3 refuses that line:SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? - Curly quotes copied from a chat or a document.
print(“Coffee with milk”)is refused withSyntaxError: invalid character '“' (U+201C). Python only accepts the straight quotes"and'. - Capital letters.
Print("Coffee")fails withNameError: name 'Print' is not defined. Did you mean: 'print'?. To Python,Printandprintare two different names. Unlike a syntax mistake, this error appears only when Python reaches that line, so the lines above it have already run. - Typing a terminal command inside the REPL. If you type
python3 stall.pyafter>>>, you getSyntaxError: invalid syntax. Close the REPL withexit()first, then run the command in the terminal.
TL;DR: Python is an interpreted language: python3 stall.py runs the .py file directly, with no compile step (turning the code into a program file first). Python checks how the whole file is written (its syntax) first, then runs its instructions from top to bottom. So if there is one syntax mistake, no line runs. print(...) writes to the terminal (the window where you type commands), and a comma inside it becomes a space. The REPL (type python3, and >>> appears) shows the result of each line you type. A script (code in a .py file) shows only what you print. Spaces at the start of a line (indentation) matter: one space in the wrong place causes IndentationError: unexpected indent. PEP 8 is the style guide for Python code: indent with 4 spaces, snake_case names (lowercase with underscores), at most 79 characters per line.
⚡ One line, one instruction
Write:
print("Mr. Darto's Coffee Stall")
print("Coffee with milk", 15)
print(15 * 3) # 3 cups
Meaning: print(...) writes to the terminal (the window for typing commands), then moves to a new line. A comma inside it becomes one space. 15 * 3 is calculated first (the star means times), then its result, 45, is written. Text goes in quotes, numbers don't. Anything after # is a comment and doesn't run.
⚡ Run right away
Guess first: in a Python file, line 3 has a syntax mistake (a mistake in how it is written). Does line 1 still run?
No. Python checks how the whole file is written first, then runs its instructions. If there is one syntax mistake, no line runs.
Meaning: Python is an interpreted language: the command python3 stall.py, typed in the terminal, runs the .py file called stall.py directly. There is no compile step (turning the code into a program file first) as in C or Go. The lines of the file run one by one, from top to bottom.
⚡ A script only shows what you print
Guess first: the file stall.py has the line 15 * 3 with no print. What shows in the terminal?
Nothing. The result is still calculated, then thrown away. Want to see it? Write print(15 * 3).
Meaning: In the REPL (type python3 in the terminal and >>> appears), the result of each line you type is shown right away. A script (code in a .py file) is different: it shows only what you print.
⚡ Spaces at the start of a line mean something
Meaning: Python uses the spaces at the start of a line to group lines into one block. For now, each line starts at the far left, with no space before it. One space in the wrong place is enough to cause IndentationError: unexpected indent (a space at the start of a line where none should be).