To predict the output of the given Python code, do not try to “see” the answer all at once. Read it in execution order, keep track of each variable’s current value, and write down every value sent to print(). This method works for short beginner exercises and for longer snippets that combine loops, conditions, and expressions.
The core rule: execute, do not guess
Python runs statements in order unless a control structure changes that path. Your job is to simulate that execution carefully.
Use a small trace table as you read:
| Step | Line or action | Current values | Printed output |
|---|---|---|---|
| 1 | score = 4 | score = 4 | |
| 2 | score = score + 3 | score = 7 | |
| 3 | print(score) | score = 7 | 7 |
For this code:
``python
score = 4
score = score + 3
print(score)
``
the output is:
``text
7
``
The second assignment does not display anything. It replaces the old value of score with a new one. Only print(score) produces visible output.
A useful habit is to separate what the computer stores from what the computer displays. Variables store values; print() displays values.
Step 1: Start with variables and assignments
When you encounter an assignment, update your trace immediately.
``python
pet = "cat"
pet = "dog"
print(pet)
``
Output:
``text
dog
``
The variable does not remember both values. After the second assignment, pet refers to "dog".
Now look at an assignment that uses the variable’s old value:
``python
points = 10
points = points - 4
points = points * 2
print(points)
``
Trace it:
| Statement | points after the statement |
|---|---|
points = 10 | 10 |
points = points - 4 | 6 |
points = points * 2 | 12 |
Output:
``text
12
``
Read points = points - 4 from right to left: calculate the expression on the right using the current value, then store the result back in points.
Step 2: Evaluate expressions before updating a variable
Python evaluates the right-hand side of an assignment before making the assignment.
``python
x = 5
y = x + 2
x = 1
print(x, y)
``
Output:
``text
1 7
``
When Python runs y = x + 2, x is still 5, so y becomes 7. Changing x later does not retroactively change y.
This is a common source of wrong answers: readers see the final value of x and accidentally use it in an earlier line.
Watch the order inside an expression
Multiplication and division happen before addition and subtraction.
``python
result = 2 + 3 * 4
print(result)
``
Output:
``text
14
``
Python calculates 3 * 4 first, then adds 2.
Parentheses can change the result:
``python
result = (2 + 3) * 4
print(result)
``
Output:
``text
20
``
When predicting output, calculate parenthesized expressions first, then multiplication or division, then addition or subtraction.
Step 3: Trace every loop iteration
A for loop repeats its body once for each item in the sequence. Do not compress several iterations into one mental step.
```python total = 0
for number in [2, 4, 6]: total = total + number
print(total) ```
Trace table:
| Loop value | total before addition | total after addition |
|---|---|---|
| 2 | 0 | 2 |
| 4 | 2 | 6 |
| 6 | 6 | 12 |
Output:
``text
12
``
The loop body runs three times. print(total) is not indented, so it runs after the loop finishes.
Indentation matters because it determines which statements belong to the loop:
```python for number in [1, 2, 3]: print(number)
print("done") ```
Output:
``text
1
2
3
done
``
The first print() is inside the loop and runs three times. The final print() is outside the loop and runs once.
Understand range() before tracing it
For range(start, stop), Python includes start but excludes stop.
``python
for n in range(2, 5):
print(n)
``
Output:
``text
2
3
4
``
Write the actual values beside the loop before tracing its body. Here, write 2, 3, 4; do not assume 5 is included.
Step 4: Test conditions with the current values
An if statement chooses a path. Evaluate its condition at the moment Python reaches it, using the values that exist then.
```python temperature = 18
if temperature > 20: print("warm") else: print("cool") ```
Output:
``text
cool
``
Since 18 > 20 is false, Python skips the first block and runs the else block.
For conditions inside loops, test the condition separately on every iteration:
``python
for n in [1, 2, 3, 4]:
if n % 2 == 0:
print(n)
``
Output:
``text
2
4
``
The % operator returns the remainder. A number is even when its remainder after division by 2 is 0.
Here is the full trace:
n | n % 2 == 0 | Output |
|---|---|---|
| 1 | false | |
| 2 | true | 2 |
| 3 | false | |
| 4 | true | 4 |
Avoid treating an if condition as a label for the whole loop. It is tested again each time the loop body runs.
Step 5: Be precise about print()
print() has its own formatting behavior. When you pass multiple values separated by commas, Python normally puts spaces between them.
``python
name = "Milo"
age = 3
print(name, age)
``
Output:
``text
Milo 3
``
A string and a number can be printed together this way because print() formats each supplied value for display.
Compare that with string concatenation:
``python
name = "Milo"
age = 3
print(name + str(age))
``
Output:
``text
Milo3
``
Here str(age) converts the number to text, and + joins the two strings without adding a space.
Also notice the difference between printing inside and outside a loop:
```python count = 0
for n in range(3): count = count + 1 print(count)
print("final:", count) ```
Output:
``text
1
2
3
final: 3
``
There are four output lines because the indented print(count) runs once per iteration.
A complete example: variables, a loop, and a condition
Try tracing this code before reading the explanation:
```python total = 0
for n in range(1, 5): if n % 2 == 1: total = total + n
print(total) ```
First, expand range(1, 5) into 1, 2, 3, 4.
n | Is n odd? | total after this iteration |
|---|---|---|
| 1 | yes | 1 |
| 2 | no | 1 |
| 3 | yes | 4 |
| 4 | no | 4 |
Output:
``text
4
``
Only 1 and 3 meet the condition, so only those values are added. The final print(total) runs after the loop.
A fast checklist for Python output questions
Before committing to an answer, check these points:
- Did I update every variable after each assignment?
- Did I use the variable values from that exact moment, not their later values?
- Did I list every loop iteration, including the endpoint behavior of
range()? - Did I test each
ifcondition separately during the loop? - Did I count every
print()statement and note its indentation? - Did I preserve spaces, line breaks, and punctuation in the output?
If one of those answers is no, trace the code again. Most mistakes come from skipping a state change, not from difficult Python syntax.
Practice without looking at the answer too soon
Predicting code output is a reasoning skill: you form a state, apply one rule, update the state, and repeat. The best practice is short, varied snippets where you must commit to an answer before seeing the result.
Use PurrLearn’s Python output quiz to practice reading code line by line. When you miss an item, identify the exact step where your trace diverged from Python’s execution; that is more useful than simply memorizing the correct output.
FAQ
How can I predict Python output without running the code?
Simulate it on paper or in a trace table. Record the value of each variable after assignments, list loop iterations, test conditions, and copy each value passed to print() in order.
Does Python run code from top to bottom?
Usually, yes. It runs statements in sequence, but loops repeat an indented block and conditions may skip blocks. Functions, exceptions, and other features can add complexity, but top-to-bottom tracing is the right starting model for beginner snippets.
Why is the last number in range(1, 5) not 5?
The second argument of range() is a stopping point, not an included endpoint. range(1, 5) produces 1, 2, 3, and 4.
What is the most common mistake in Python output questions?
Using a variable’s final value when evaluating an earlier line is especially common. The fix is simple: write down the variable’s value after every statement that changes it.
Should I memorize Python output patterns?
Memorize a few rules, such as assignment order, indentation, operator precedence, and range() endpoints. For each new snippet, however, trace the actual execution instead of relying on a pattern that only looks similar.