What is Python?

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…

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

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:

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:

An example in the REPL:

$ python3
>>> 15 * 3
45
>>> "Coffee with milk"
'Coffee with milk'
>>> print("Coffee with milk")
Coffee with milk
>>> exit()

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

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:

🎯 What Python is used for

⚠️ Why does your first program fail right away?

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).

What you will learn