How to trace Python code by hand
Tracing means predicting exactly what a program prints. Learn the discipline: track variables, follow loops, and evaluate expressions in order.
Tracing a program means working through it one line at a time, like the computer does, and writing down the value of every variable after each step. The output is whatever the print calls produce, in the order they run. A trace table stops you from keeping it all in your head.
The method step by step
- Make a table. Write one column for each variable, plus a column for output if the program prints inside a loop.
- Go line by line. Start at the top and run exactly one statement at a time.
- Evaluate the right side first. In
x = x + 2, work outx + 2using the old value ofx, then store the result. - Update the table each time a variable changes. Add a new row, or cross out the old value.
- For loops: list the values the loop variable takes.
range(2, 9, 3)starts at 2, adds 3 each time, and stops before 9. So it gives 2, 5, 8. - For while loops: test the condition before every pass, including the first. When the condition is false, skip the body and carry on after it.
- For if statements: test the condition, run only the branch that matches, and skip the rest.
- Print only what print shows. A variable that changes but is never printed does not appear in the output.
Worked example 1: assignments in sequence
a = 6
b = 4
a = a + b
b = a - b
print(a, b)
Trace it step by step:
- After line 1, .
- After line 2, .
- Line 3 works out and stores it in , so .
- Line 4 works out and stores it in , so .
- The print shows the new values: 10 6.
The mistake to avoid is using the old on line 4. By then is 10, not 6.
Worked example 2: a for loop with a step
total = 0
for i in range(2, 9, 3):
total += i
print(i, total)
print(total)
The loop variable takes the values 2, 5 and 8. Next is 11, which is not below 9, so the loop stops.
- Pass 1: , . It prints 2 2.
- Pass 2: , . It prints 5 7.
- Pass 3: , . It prints 8 15.
- After the loop, the last print gives 15.
The output is four lines. The print inside the loop runs three times and the print after the loop runs once, which is why indentation matters.
Worked example 3: a while loop with if and else
n = 6
steps = 0
while n > 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
steps += 1
print(steps, n)
Here % gives the remainder after division, and // is whole-number division. Start with and .
- is even, so . .
- is odd, so . .
- is even, so . .
- is odd, so . .
- is even, so . .
- is even, so . .
- is even, so . .
- is even, so . .
- , and is false, so the loop ends.
- The print shows 8 1.
The same trace written as a trace table (one row per pass of the loop):
pass n before branch n after steps
1 6 even, n // 2 3 1
2 3 odd, 3n + 1 10 2
3 10 even 5 3
4 5 odd 16 4
5 16 even 8 5
6 8 even 4 6
7 4 even 2 7
8 2 even 1 8
The condition is checked at the start of every pass, so the loop never runs with .
Worked example 4: a function and a list
def f(xs):
out = []
for x in xs:
if x % 2 == 1:
out.append(x * 2)
else:
out.insert(0, x)
return out
print(f([3, 4, 5, 8]))
The def line only defines the function. Nothing in it runs until the call on the last line. Then xs is the list [3, 4, 5, 8] and out starts empty.
- is odd, so
append(6). . - is even, so
insert(0, 4)puts 4 at the front. . - is odd, so
append(10). . - is even, so 8 goes to the front. .
- The function returns the list and print shows [8, 4, 6, 10].
Always write the list out in full after each change. Keeping it in your head is where most errors creep in.
Common mistakes
- Using the final value of a variable in an earlier step. Each line sees the value as it is at that moment.
- Going one step too far in
range.range(2, 9)stops at 8, not 9. - Forgetting that
range(5)starts at 0 and gives 0, 1, 2, 3, 4. - Treating
=as "equals" in maths. It means "store the value on the right in the name on the left". - Mixing up
/and//. In Python 3,7 / 2is 3.5 and7 // 2is 3. - Executing the lines of a function when it is defined instead of when it is called.
- Missing indentation. Only indented lines belong to the loop or the if.
- Skipping the output. A trace question asks for what is printed, not for the variables.
Exam tips
- Always draw a trace table, even for short programs. Check every row once at the end.
- Write loop variable values in advance, so the loop count is clear.
- For lists, write the whole list after each change.
- Mark off lines as you complete them.
- Check types. Printing
"5" + "5"gives55, not 10, because the quotes make them strings.
Frequently asked questions
What is a trace table?
A trace table has one column for each variable and one row for each step. It records how the values change as the program runs.
Does print show variables that were not printed?
No. The output is only what print calls produce. Changing a variable does nothing visible by itself.
What does range(1, 5) give?
It gives 1, 2, 3 and 4. The start is included and the end is not.
How do I know when a while loop stops?
It stops when the condition is false at the start of a pass. If the condition is never false, the loop runs forever.
Why does indentation matter?
Python uses indentation to decide which lines belong to a loop, an if or a function. A line with less indentation is outside it.
For more practice, try the code tracing questions here and write the table before you look at the options.
Put it into practice
Practise this topic with questions and get an explanation after each answer.
Practise Code tracing: "what does this print?" →