Accel Thinking

Python Learning Notes

What Is a Dry Run? Trace Your Code Before Running It

Trace code with small inputs and variable tables to catch boundary, initialization, and update-order mistakes.

Many students finish some code, press Run immediately, and add several print() statements when the answer is wrong. In algorithm problems, there is another habit that often saves more time: take a sample input and execute the code yourself on paper before running it.

This is called a dry run, or code tracing. It can expose boundary, initialization, and update-order mistakes before submission.

What Happens in a Dry Run?

A dry run is not a guess. You follow the program one line at a time and record the current values of important variables.

Suppose we want to find the largest value in a list:

numbers = [3, 1, 5, 2]
largest = numbers[0]

for number in numbers:
    if number > largest:
        largest = number

print(largest)

Trace it with a table:

Current number largest before number > largest largest after
3 3 False 3
1 3 False 3
5 3 True 5
2 5 False 5

The final result is 5. More importantly, the table shows how the algorithm’s state changes instead of showing only its final output.

Why Does the Variable Table Help?

Algorithm mistakes are often logic errors, not syntax errors. The code runs, but one iteration updates the wrong value or an edge case produces an incorrect answer.

For every iteration, the table makes you identify the current input, the state before the decision, whether the condition is true or false, and the state after the update. When the trace first differs from your expectation, you have found the neighborhood of the bug.

Finding an Initialization Bug

Consider this version:

numbers = [-4, -2, -7]
largest = 0

for number in numbers:
    if number > largest:
        largest = number

print(largest)

Every comparison is false, so the program prints 0 even though 0 is not in the input. The loop is not the problem. Initializing largest to 0 introduced an invalid candidate when all values were negative.

Initialize from the data instead:

numbers = [-4, -2, -7]
largest = numbers[0]

for number in numbers[1:]:
    if number > largest:
        largest = number

print(largest)

The new trace finishes with -2. A small edge-case input revealed a mistake that an ordinary positive example would hide.

Checking Update Order

A dry run also catches mistakes when one iteration updates several variables:

a = 2
b = 5

a = b
b = a

After a = b, both values are already 5. The second assignment cannot recover the old value of a. Tracing each assignment shows exactly where that information was lost. In Python, the swap can be written as a, b = b, a.

A Five-Step Dry Run

  1. Choose a small but representative input.
  2. Identify the variables that can change.
  3. Create one table row for every loop iteration.
  4. Update values in the exact order used by the code.
  5. Repeat with an edge case such as a minimum input, duplicates, all-negative values, or a value exactly on a boundary.

The input should be small enough to trace clearly but varied enough to make important conditions become both true and false.

Dry Run vs. Runtime Debugging

Method When to use it Best at finding
Dry run Before running or submitting Idea, boundary, initialization, and update-order errors
print() debugging While the program runs Actual execution paths and values
Debugger When step-by-step pauses are useful More complex calls and program state

These methods complement one another. A useful sequence is to trace the idea first, run tests second, and then gather runtime evidence if a problem remains.

Practice

  1. Trace the maximum-finding code with numbers = [4, 2, 6] and record number and largest .
  2. Test the largest = 0 version with numbers = [-3] and explain the failure.
  3. Challenge: dry-run a loop that sums a list, then design an edge case for an empty list.

One-Sentence Takeaway

A dry run uses a small input to trace code and record changing variables, revealing boundary, initialization, and update-order errors before execution.