Write and Run Your First Program

A computer does nothing on its own: it follows instructions that someone wrote down. Those instructions live in a file, and Python is what reads it.

A program is instructions in a file

A program is a list of instructions saved in a file. The file is ordinary text, the kind you could open in any text editor, and each line usually holds one instruction. A file of Python instructions has a name that ends in .py, such as hello.py.

The instructions do not run by themselves. Python is itself a program, called an interpreter, whose job is to read a file like this and carry out what it says. It starts at the first line, does what that line asks, moves on to the next line, and stops when it reaches the end of the file. So the order of the lines is the order in which things happen.

hello.py
print("Good morning")
print("Time to study")
print("See you later")
What appears on the screen
Good morning
Time to study
See you later

Swap the first and last lines in the file, and the screen shows the same three lines in the new order. Python never skips ahead or rearranges anything: it carries out the lines one by one, from top to bottom.

True or false?

Python runs the lines of a file in whatever order it finds best.

False. Python carries out the lines in the order they are written, from the top of the file to the bottom.

A Python program is a plain text file that you could open and read in any text editor.

True. The program is just text. It only does something when Python reads it and carries it out.

The terminal and the folder it works in

To ask Python to run a file, you type a command in a terminal. A terminal is a window where you type commands as text instead of clicking on icons. On macOS and Linux the app is called Terminal; on Windows it is PowerShell, often opened through the Terminal app. You type a command, press Enter, the computer carries it out, and any result is printed as text in the same window.

A terminal always works inside one folder at a time, called the current folder (its official name is the working directory). When a command mentions a file such as hello.py, the computer looks for it in the current folder. If the file sits in a different folder, the command cannot find it, even though the file exists.

  • robin your home folder
  • Documents another folder
  • projects
  • study current folder
  • hello.py

Two commands let you find your way. pwd prints the current folder, and cd (short for "change directory") moves into another folder. cd projects moves into the folder projects inside the current one, and cd .. moves one level up. Both commands work the same way in the terminals of macOS and Linux and in PowerShell on Windows.

Moving into a folder
$ pwd
/home/robin
$ cd projects
$ cd study
$ pwd
/home/robin/projects/study

The $ at the start of a line is the prompt: the terminal's sign that it is waiting for a command. You type what comes after it, never the $ itself. Your prompt may look different and often shows the current folder, but it means the same thing. A command such as cd that succeeds usually prints nothing; the next prompt simply appears.

True or false?

If a terminal is in the folder robin and hello.py is in robin/projects/study, typing a command that names hello.py finds the file anyway.

False. A command looks for a file in the current folder. Move into study with cd first, and then the file is found.

The command cd .. moves the terminal one folder up, to the folder that contains the current one.

True. Two dots stand for the folder one level up.

Running a Python file with uv

Python comes in versions, and a program written for a newer version may not run on an older one. Instead of installing Python by hand and hoping the right version is there, you can let a project tool handle it. uv is such a tool: you install uv once, and it makes sure each project runs with the Python version that project asks for.

A project here is simply a folder for one program. uv recognises a project by a file called pyproject.toml inside it. That file holds the project's name and the Python versions it accepts. A second small file, .python-version, names the version uv should use, such as 3.15. You do not write these files by hand: the command uv init --no-package creates both in a new project folder, together with a first Python file. The --no-package part keeps the project the simplest kind: a plain folder with your Python files right in it.

To run a Python file of the project, move the terminal into the project folder and type uv run followed by the file name:

$ cd study
$ uv run hello.py
Good morning
Time to study
See you later
  • You type uv run hello.py
  • uv finds the Python the version .python-version names, downloaded if missing
  • Python runs the file top to bottom
  • Lines on the screen
  • You type to uv finds the Python
  • uv finds the Python to Python runs the file
  • Python runs the file to Lines on the screen

Behind that one command, uv reads which Python the project asks for, looks for it on the computer and downloads it if it is not there yet, and then hands hello.py to that Python. The first time you run a command like this in a project, uv also creates a folder called .venv. This folder is the project's own environment: the place where uv keeps what the project needs. You never have to open or prepare that folder yourself: uv run checks it and keeps it up to date every time.

True or false?

Before uv run can work, you must install the right version of Python yourself.

False. By default uv downloads the Python version the project asks for when it is not found on the computer.

uv run hello.py only finds hello.py when the terminal is in the folder that holds it.

True. The file name is looked up from the current folder, like any other file a command names.

When Python cannot read the file

Python reads a file by strict rules: every bracket that opens must close, every string that starts with a quote must end with one, and so on. These rules of how Python code must be written are called its syntax. A single mistyped character can break them, and then Python cannot make sense of the line.

Before Python carries out any instruction, it first reads through the whole file to check that it can understand every line. Only then does it start running the lines from the top. A mistake that breaks the rules is called a syntax error, and when Python finds one, it refuses to run the file at all. So a syntax error on line 2 stops the program before line 1 has run. Instead of the program's output, you see a message that says what went wrong and where. Here is a file whose second line is missing its closing quote:

hello.py
print("Good morning")
print("Time to study)
Running it
$ uv run hello.py
  File "/home/robin/projects/study/hello.py", line 2
    print("Time to study)
          ^
SyntaxError: unterminated string literal (detected at line 2)

Read the message from the top. The first line names the file and the line number, counting the first line of the file as line 1, so you know exactly where to look. Below it, Python repeats that line and puts a small arrow, ^, under the place where it noticed the problem. The last line names the error: SyntaxError, followed by a description. "Unterminated string literal" means a string was opened and never closed.

Notice that Good morning was not printed, even though line 1 is correct. Python refused the whole file.

The arrow shows where Python noticed the mistake, which is not always where the fix goes. Here it points at the opening quote, because that is where the unfinished string begins; the missing quote belongs at the end of the text, just before the closing bracket. So use the line number to find the line, then read the whole line, comparing it with the rules. After you fix it and run the file again, both lines are printed.

True or false?

When line 2 of a file has a syntax error, line 1 still runs and its output appears before the error message.

False. Python reads the whole file before running anything. A syntax error anywhere stops every line from running.

The line number in a syntax error message tells you which line of the file to look at.

True. The message names the file and the line, counting from 1 at the top, and repeats that line underneath.

The fix always goes exactly where the small arrow points.

False. The arrow marks where Python noticed the problem. For a string that was never closed, it points at the opening quote, while the missing quote belongs further along the line.