Getting started
By the end of this chapter you will have Python installed, and you will have written and run your first program.
Install Python
- Windows: download the latest Python 3 installer from python.org/downloads. On the first screen, tick Add python.exe to PATH before clicking Install.
- macOS: download the installer from the same page, or use Homebrew with
brew install python. - Linux: Python 3 is usually installed already. If not, use your package manager, for example
sudo apt install python3.
Check that it worked by opening a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and running:
python --version
You should see something like Python 3.12.4. On macOS and Linux the command may be python3 instead of python.
Pick an editor
You can write Python in any text editor, but a code editor helps a lot. Visual Studio Code is free and popular; install its Python extension and it will highlight mistakes as you type.
Your first program
Create a folder for your work, and inside it a file called hello.py containing:
print("Hello, world!")
In the terminal, move into that folder with cd and run the file:
python hello.py
Python reads your file from top to bottom and runs each line. print is a built-in function that shows whatever you give it. The text inside quotes is a string.
Comments
Anything after a # on a line is a comment. Python ignores it; it is a note for humans.
# Greet the user
print("Hello, world!") # this also works at the end of a line
The interactive shell
Run python with no file name and you get the interactive shell, shown by the >>> prompt. Type a line and Python runs it immediately.
>>> 2 + 3
5
>>> 7 * 6
42
>>> print("hi")
hi
The shell is perfect for quick experiments. Type exit() to leave it.
When things go wrong
Mistakes are normal. Python stops and prints an error that tells you what went wrong and on which line:
print("Hello)
SyntaxError: unterminated string literal (detected at line 1)
Read the last line of an error first. It names the problem; the lines above show where it happened.
Exercises
1. Three lines
Write a program that prints your name, your favourite programming language and the year on three separate lines.
Show answer
print("Ada")
print("Python")
print(2026)
Each print call ends with a new line. Numbers do not need quotes.
2. Shell arithmetic
In the interactive shell, find out what 7 / 2, 7 // 2 and 7 % 2 give. What do you think each operator does?
Show answer
7 / 2 is 3.5 (normal division). 7 // 2 is 3 (floor division: divide and round down). 7 % 2 is 1 (the remainder).
3. Break it on purpose
Remove the closing bracket from print("Hello, world!") and run the file. Read the error, then fix it.
Show answer
You get a SyntaxError saying a ( was never closed. Learning what common errors look like now will save you hours later.