How Python Reads and Runs Code
Learn how Python follows instructions in order, evaluates expressions, and makes results visible with print(). Trace a short program line by line and keep its source code separate from its output.
For the small programs in this chapter, Python begins with the first line and works downward. It runs one instruction before moving to the next.
This gives us an important distinction:
Python does not run your intention. It runs the exact code it receives.
If the code says print(2 + 3), Python adds the numbers before displaying 5.
If the code contains something Python cannot understand or run, Python reports
an error. The error gives us information about where execution stopped.
The built-in function print() displays a value as text. A function is a
named piece of code that performs a task. In this book, displayed output appears
below the code that produced it.
Code Is a Sequence of Instructions
Consider this program:
Python runs the lines in order:
first
second
third
Line order
Run the program, then edit the words and run it again.
Ready to run.
We can trace the program without running it:
| Line | Instruction | New output |
|---|---|---|
| 1 | display first | first |
| 2 | display second | second |
| 3 | display third | third |
The output has the same order as the instructions because the three calls run from top to bottom.
If you edit "first" to another word and run the program again, the output
changes with the code. Python runs the program that exists at that moment, not
an earlier version or an unstated intention.
Expressions Produce Values
An expression is code that produces a value. This arithmetic expression
produces the number 5:
2 + 3
Evaluating an expression means working out its value. In a script, Python does
not automatically display that value. You usually pass it to print() when
you want to see it:
print(2 + 3)
This distinction matters later. A program may compute a value correctly without displaying it. The program can print the value, plot it, save it, or pass it to another step.
The following program evaluates 2 + 3 twice. The first result is not sent to
the output. The second result is passed to print(), so it becomes visible.
A value and visible output
Run the program, then change both additions and compare what appears.
Ready to run.
Printing More Than One Value
print() can receive more than one value:
print("loss:", 0.42)
Python first evaluates each value inside the parentheses. By default, print()
places a space between them, so the output is:
loss: 0.42
You will see this pattern in later experiments. A script computes something, then prints selected values so you can inspect what happened.
Code Is Not Output
Code and output play different roles.
Code:
print(2 + 3)
Output:
5
The output does not contain print, the parentheses, or the expression
2 + 3. Those are parts of the source code. The output contains the value that
the program displayed after evaluating the expression.
What does this program display?
Select one choice, then check.
HintSeparate code from output
Follow the three print() calls from top to bottom. The middle call prints
the value produced by 2 + 3, not the expression's source text.
SolutionEvaluate, then print
The first call prints start. The second evaluates 2 + 3 and prints 5.
The third prints done. Each call ends its output with a new line.
A Useful Habit
When you read a small program, keep the code and its output separate. Predict the output from the code, run the program, and compare the actual output with your prediction. A difference between them points to something in the code or in your understanding that is worth inspecting.
The next lesson shows how to check whether a Python runtime is available on your computer and how to install one if it is not.