Using the print() Function
A print() statement displays a result.
A Script Has an Order
A script is not treated as one undifferentiated action. Python runs its statements sequentially, from top to bottom. Each statement completes before the next statement begins. This order matters because an earlier statement can store a value that a later statement uses or displays.
Storing Versus Displaying
Two statements can have very different visible effects. An assignment statement stores a value in memory, but it produces no output on the screen. A print() statement displays a result. Therefore, a script may perform an action without showing anything, and then display a value later when a print() statement runs.
| Statement role | What happens | Visible output |
|---|---|---|
| Assignment | A value is stored for later use. | No |
| print() | A result is displayed. | Yes |
The key distinction when tracing a script
Tracing a Short Script
To predict a script's output, inspect one statement at a time. For each line, ask two questions: What does this statement do, and does it produce output? Also note whether a variable receives a value that can be used later.
message = "Ready"
print(message)
print("Go")- 1The first statement stores the value Ready in message. It does not display output.
- 2The second statement displays the value stored in message, so the first visible line is Ready.
- 3The third statement displays Go, so the second visible line is Go.
Ready
GoPredict Before Reading the Output
What do you think happens?
What visible output is produced by this script?
count = 2
print(count)
count = 3
print(count)Reveal answer
2, then 3
The first assignment stores 2 without displaying it. The first print() displays 2. The next assignment stores 3, and the final print() displays the updated value 3. The statements are traced in their top-to-bottom order.
This trace shows why execution order and stored values must be considered together. A value assigned earlier remains available for later use, but a later assignment can change what a subsequent print() statement displays. The assignment itself remains invisible; only the print() statements create visible output.
Mistakes in Output Tracing
Common mistake Treating every statement as visible output.
What it looks likename = "Ava"Why it failsAn assignment stores a value but does not produce output.
Do this insteadCount a line as visible output only when it is a print() statement.
Common mistake Reading the script as if all statements run at once.
What it looks likevalue = 1 print(value) value = 2 print(value)Why it failsStatements execute from top to bottom, with each statement completing before the next begins.
Do this insteadTrace each line in order and record the value available at that point.
Common mistake Ignoring a later assignment when predicting a later display.
What it looks likelevel = 1 level = 2 print(level)Why it failsThe value available to the final
print()is the value stored by the later assignment.Do this insteadWhen tracing, update your notes whenever an assignment stores a value.
Practice the Line-by-Line Method
Trace this script from top to bottom. Write down which statements produce output and what appears on the screen, in order.
first = "Start"
print(first)
second = "Finish"
print(second)Hints
- The first and third statements are assignments.
- The second and fourth statements are
print()statements. - The value assigned to first is displayed before the value assigned to second.
A complete trace
Determine the visible output of the practice script.
- 1Statement 1
first stores Start. There is no visible output.
- 2Statement 2
print(first)displays Start. - 3Statement 3
second stores Finish. There is no visible output.
- 4Statement 4
print(second)displays Finish.
The visible output is Start, followed by Finish.
Key Takeaways
- 1Python executes statements sequentially from top to bottom.
- 2Each statement completes before the next statement begins.
- 3Assignment statements store values but produce no visible output.
- 4
print()statements display results. - 5To predict output, trace every statement in order and track which values are available at each point.
At a glance
- A script executes one statement after another, from top to bottom.
- Assignments store values without displaying them.
print()displays a result when its statement runs.- Tracing means recording each statement's action, visible output, and effect on available values.