A program often contains more than one instruction. In a simple sequence like the one below, Python executes the statements from top to bottom.
print("Preparing report")
print(4 + 3)
print("Report ready")
Preparing report
7
Report ready
Each call to print displays its result and, by default, ends the line. The addition is evaluated before the second call displays its result.
Trace the path
A trace is a step-by-step account of what a program does. For this example:
| Step | Instruction | What appears |
|---|---|---|
| 1 | print("Preparing report") |
Preparing report |
| 2 | print(4 + 3) |
7 |
| 3 | print("Report ready") |
Report ready |
The table follows the code in the order Python encounters it. If we move the final line to the top, the output changes order too. That matters when messages describe stages of a process: displaying “ready” before “preparing” would give a misleading account of what the program is doing.
This top-to-bottom model applies to the simple, uninterrupted statements here. Later, we will meet constructs that change the path through a program, such as decisions and loops. An error can also prevent the expected sequence from completing.
Code is not the same as output
The instruction print(4 + 3) contains an operation and a function call. The displayed 7 is their result. When inspecting an example, keep those two views separate: the code tells Python what to do; the output lets us observe part of what happened.