BasicsChapter 2 of 114
Get Started
Install Python, run a file, and use the interactive prompt.
Check what you already have
Many machines ship with Python. Open a terminal and ask:
python --versionIf that prints something like Python 3.13.1, you are ready. If it prints a version starting with 2., try python3 --version instead — some systems keep the old Python 2 on the python name.
If the command is not found, download an installer from python.org/downloads.
Run a file
A Python program is a plain text file ending in .py. Make one called hello.py:
print("Hello from a file")
print("Line two runs after line one")Output
Hello from a file Line two runs after line one
Then run it from the same folder:
python hello.pyThe interpreter reads the file from the top, runs each line, and exits. There is no main function you have to write and nothing to declare first.
The interactive prompt
Type python on its own and you get a prompt instead:
>>> 2 + 2
4
>>> name = "Ada"
>>> name.upper()
'ADA'
>>> exit()This is the REPL — read, evaluate, print, loop. It runs each line as you press Enter and shows the result without you asking, which makes it the fastest way to check what something does.
An editor
Any text editor works. VS Code with the Python extension is the common choice and is free — it will offer to install the extension the first time you open a .py file.
What you want from an editor is syntax colouring and a way to run the file without leaving the window. Everything beyond that is preference.
Or use this page
The examples here run in your browser on a real Python interpreter compiled to WebAssembly. Nothing is sent to a server, and the code you type is yours to break:
for i in range(3):
print("run", i)Output
run 0 run 1 run 2
Test yourself
2 questionsWhat does a .py file need at the top to run?
Show the answer
Nothing at all — Python reads the file from the top and runs each line. There is nothing you must declare first.
Why does 2 + 2 show 4 at the >>> prompt but print nothing in a file?
Show the answer
The REPL prints the value of an expression, a file does not — In a file you need print() to see anything. This catches almost everyone once.
Syntax
Indentation is the structure. Here is what that means in practice.