Every other lesson assumes you can try code quickly and run a file from the terminal.
Python is an interpreted language: you hand source code to the python program and it runs it directly, with no separate compile step you have to manage. There are two everyday ways to do that.
The REPL (read-eval-print loop) starts when you type python3 (or py on Windows) with no arguments. You type an expression, press Enter, and the value is printed back. It is the fastest way to check how something behaves. Python 3.13 ships a much nicer REPL with colour, multi-line editing and history; exit or Ctrl-D leaves it.
A script is just a text file ending in .py. Run it with python3 hello.py. The file executes top to bottom; nothing is printed automatically, so you use print() to show output.
python3 --versiontells you which interpreter you are running; this course targets 3.13.python3 -m module_nameruns a standard-library or installed module as a program, e.g.python3 -m http.server.python3 -i script.pyruns the script and then drops you into the REPL with its variables still defined, which is handy for poking at results.
Indentation is part of the syntax: blocks are defined by consistent leading spaces (use four), not braces. Comments start with # and run to the end of the line.
A common idiom at the bottom of a script is if __name__ == "__main__":. Code under it runs when the file is executed directly but not when another file imports it, which lets one file be both a reusable module and a runnable program.
Example
# hello.py -- run with: python3 hello.py
import sys
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("world")) # Hello, world!
print(sys.version_info[:2]) # e.g. (3, 13)Remember
python3alone opens the REPL;python3 file.pyruns a script- Indentation (four spaces) defines blocks
if __name__ == "__main__":guards code that should only run as a script
Watch out for
- Mixing tabs and spaces for indentation, which raises
TabErroror produces confusing blocks.