Tracing Code Manually

Concepts covered: Conditional Loops

Tracing is running the program yourself: a table with a column per variable, and a new row each time a line changes one. It's slow, which is the reason it works. Reading code lets you believe what it does; tracing it shows what it does, and the 2 differ more often than anyone expects.

Swapping 2 values in 3 lines

Start with 6 lines and no loop. Before pressing play, write down what you expect x and y to be at the end, then watch each line change the stage.

x = 5
y = 3
x = x + y
y = x - y
x = x - y
print("x =", x, "y =", y)
x = 3 y = 5
Linexy
x = 55
y = 353
x = x + y83
y = x - y85
x = x - y35

The third line reads x on the right before it writes x on the left, so x + y is 5 + 3 and the new x is 8. The fourth line then uses that 8, and 8 minus 3 is 5. The trace shows the swap works because each right side reads the values the previous row left behind. Someone who skims the code and sees 'add then subtract' usually predicts the values end unchanged.

Tracing a loop

A loop adds 1 row per pass. This program peels the digits off a number from the right: num % 10 is the last digit and num // 10 drops it. Trace it with 3 columns before running it.

num = 123
total = 0
while num > 0:
    digit = num % 10
    total = total + digit
    num = num // 10
print("Sum of digits:", total)
Sum of digits: 6
Passdigittotalnum after
before loop0123
13312
2251
3160
check60, so the loop stops

The loop condition is checked before every pass, and a fourth check finds num equal to 0 and stops the loop. The table has 1 numbered row per pass. Counting the numbered rows, and leaving out 'before loop' and 'check', answers 'how many times did the body run': 3.

Every trace of a while loop ends with 1 more condition check than it has passes. If you skip the check after pass 3, you run the body again with num equal to 0 and write a fourth row that never happened. The check row is what tells you to stop.

Tracing a loop over a list

With a for loop over a list, the loop variable takes each item in order, and the trace needs a column for it. This program keeps the highest score seen so far. Write the trace table yourself before playing it, then compare.

scores = [42, 17, 58, 23]
best = scores[0]
for score in scores:
    if score > best:
        best = score
print("Highest:", best)
Highest: 58
scorescore > best?best
start42
42no42
17no42
58yes58
23no58

The first pass compares 42 to itself and does nothing, which is correct and harmless. The only row that changes best is the 58 row. A trace table like this is the fastest way to answer a question such as 'what if the list were [3, 9, 9]': 2 rows say no, 1 row says yes, and best ends at 9.

Practice

Trace this program by hand. What does it print?

count = 0
for n in [4, 7, 10, 3]:
    if n % 2 == 0:
        count = count + 1
    else:
        count = count + 2
print(count)

When a trace ends on the wrong number, the first row where it goes wrong points at the line to fix. Add a column for the condition itself and write yes or no beside each item. If the total grows on rows that should say no, the test is backwards. If it grows by the wrong amount on the yes rows, the body is wrong. For an even number n % 2 is 0, and for an odd number it is 1.

About this lesson

This section is part of the Problem Solving: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.