Sub topic 1
What Python Is and How a Python Program Runs
Prerequisites
This chapter assumes you can install software on your computer and open a terminal or command prompt. You do not need to have written code before. Later chapters review terminal basics, so you can skip that for now.
What Python is
Python is a general-purpose programming language. People use it for automation, web services, data analysis, testing, and teaching. Its syntax is readable, and its standard library covers many common tasks without extra downloads.
Python is an interpreted language in the everyday sense. You do not compile your program into a separate executable before running it. Instead, a program called the interpreter reads your code and runs it. When people say "install Python," they mean installing this interpreter and its standard library.
How a program runs
When you run a Python file, the interpreter does several things in order:
- It reads your source code from the
.pyfile. - It checks the syntax and translates the code into bytecode. Bytecode is a compact, lower-level set of instructions.
- It runs the bytecode on the Python virtual machine, which is the part of the interpreter that executes instructions one by one.
- It prints output or raises an error, then exits.
CPython is the standard interpreter, and it is the one you will install. Other implementations exist, but this tutorial uses CPython throughout.
Because translation happens each time you start the program, a syntax error stops the run before any line executes. A runtime error, such as using a name that does not exist, stops the program only when execution reaches that line.
Two ways to run code
You will use the interpreter in two modes during this tutorial.
| Mode | How you start it | Best for |
|---|---|---|
| Interactive (REPL) | Run `python3` with no file | Trying one line at a time |
| Script | Run `python3 hello.py` | Programs you want to save and repeat |
REPL stands for read, evaluate, print, loop. The interpreter reads a line, evaluates it, prints the result, and waits for the next line. A script is a text file that the interpreter runs from top to bottom.
Here is the smallest useful script. You will save and run it in a later chapter:
print("Hello, world!")The interpreter reads this one line, finds the built-in function print, and sends the text to your terminal.
What you need before you can run anything
Running a Python program requires three things:
- An installed interpreter, which the next chapters cover.
- A terminal that can find that interpreter by name.
- A text file containing Python code, or a REPL session where you type it.
Most setup problems come from the second item. The terminal looks for commands in a list of folders called PATH, and a later chapter explains how to check it.
Python 2 and Python 3
Python 3 is the current and supported line of the language. Python 2 reached end of life in 2020 and no longer receives fixes. Code written for one often fails on the other. For example, print "hi" was valid in Python 2 but is a syntax error in Python 3. Install Python 3 and ignore tutorials that use Python 2 syntax.
Exercise
A script contains a correct first line, print("start"), followed by a line with a syntax error. Does "start" appear in the terminal when you run it? Why or why not?
Show answer
No. The interpreter checks the whole file and translates it to bytecode before running anything, so the syntax error stops the program before the first line executes. A runtime error, such as an undefined name, would behave differently: lines before it would run first.
Exercise
Name the two modes for running Python code and give one situation where you would choose each.
Show answer
The REPL suits trying a single expression or testing how a function behaves. A script file suits any program you want to save, edit, and run again.