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

  1. Make a table. Write one column for each variable, plus a column for output if the program prints inside a loop.
  2. Go line by line. Start at the top and run exactly one statement at a time.
  3. Evaluate the right side first. In x = x + 2, work out x + 2 using the old value of x, then store the result.
  4. Update the table each time a variable changes. Add a new row, or cross out the old value.
  5. 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.
  6. 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.
  7. For if statements: test the condition, run only the branch that matches, and skip the rest.
  8. 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:

  1. After line 1, a=6a = 6.
  2. After line 2, b=4b = 4.
  3. Line 3 works out 6+4=106 + 4 = 10 and stores it in aa, so a=10a = 10.
  4. Line 4 works out a−b=10−4=6a - b = 10 - 4 = 6 and stores it in bb, so b=6b = 6.
  5. The print shows the new values: 10 6.

The mistake to avoid is using the old aa on line 4. By then aa 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.

  1. Pass 1: i=2i = 2, total=0+2=2total = 0 + 2 = 2. It prints 2 2.
  2. Pass 2: i=5i = 5, total=2+5=7total = 2 + 5 = 7. It prints 5 7.
  3. Pass 3: i=8i = 8, total=7+8=15total = 7 + 8 = 15. It prints 8 15.
  4. 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 n=6n = 6 and steps=0steps = 0.

  1. n=6n = 6 is even, so n=6÷2=3n = 6 \div 2 = 3. steps=1steps = 1.
  2. n=3n = 3 is odd, so n=3×3+1=10n = 3 \times 3 + 1 = 10. steps=2steps = 2.
  3. n=10n = 10 is even, so n=5n = 5. steps=3steps = 3.
  4. n=5n = 5 is odd, so n=16n = 16. steps=4steps = 4.
  5. n=16n = 16 is even, so n=8n = 8. steps=5steps = 5.
  6. n=8n = 8 is even, so n=4n = 4. steps=6steps = 6.
  7. n=4n = 4 is even, so n=2n = 2. steps=7steps = 7.
  8. n=2n = 2 is even, so n=1n = 1. steps=8steps = 8.
  9. n=1n = 1, and 1>11 > 1 is false, so the loop ends.
  10. 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 n=1n = 1.

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.

  1. x=3x = 3 is odd, so append(6). out=[6]out = [6].
  2. x=4x = 4 is even, so insert(0, 4) puts 4 at the front. out=[4,6]out = [4, 6].
  3. x=5x = 5 is odd, so append(10). out=[4,6,10]out = [4, 6, 10].
  4. x=8x = 8 is even, so 8 goes to the front. out=[8,4,6,10]out = [8, 4, 6, 10].
  5. 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 / 2 is 3.5 and 7 // 2 is 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" gives 55, 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?" →