2️⃣ Tracing a code

What is Tracing?

Tracing is the systematic process of following code execution line by line, manually tracking how variable values change at each step without running the program.

Why Tracing Matters: Tracing is one of the most powerful debugging and learning tools. It helps you understand what your code actually does (versus what you think it does), predict outcomes, and identify logical errors before they become bugs.


🎯 When to Use Tracing

Use tracing in these situations:

  • Learning new concepts: Understanding how loops, conditions, or functions work
  • Debugging: Your code runs but produces unexpected results
  • Code review: Understanding someone else’s code or your own old code
  • Before running: Predicting what a program will do

📋 The Tracing Method: Four Steps

1. CREATE THE TRACE TABLE

Draw a table with these columns:

  • Step: The execution steps of the code
  • Line #: The line number being executed at this step
  • Instructions The instruction executed according to variable contents.
  • One column per variable: Each variable gets its own column

2. EXECUTE LINE BY LINE

Process each line from top to bottom:

  • Skip comments and blank lines
  • For each line, determine what happens
  • Update the table after each change

3. UPDATE VARIABLE VALUES

When a variable changes:

  • Evaluate the right side of the assignment using current values
  • Write the new value in the variable’s column
  • Use a dash (—) if a variable doesn’t exist yet

4. TRACK OUTPUT

For print statements:

  • Write what gets printed in the Output column
  • Use the current values of variables

💡 Basic Example: Variable Assignment

CODE TO TRACE:

x = 5
y = 10
x = x + y
y = x - 3
print(x, y)

TRACE TABLE:

Step Line Instructions x y
1 1 x = 5 5
2 2 y = 10 5 10
3 3 x = 5 + 10 = 15 15 10
4 4 y = 15 - 3 = 12 15 12
5 5 print(15, 12) -> 15 12 is displayed 15 12

Key Insight: On line 3, we use the old value of x (which is 5) to calculate the new value of x (which is 15). This is why x = x + y makes sense in programming!


⚠️ Common Tracing Mistakes

Error 1: Using Future Values

❌ Wrong: In x = x + 5, thinking x already equals its new value when evaluating the right side

✅ Correct: Always use the current value of x to calculate the new value

Error 2: Confusing Lines and Steps

❌ Wrong: Thinking execution always starts at the first line

✅ Correct: Make sure you understand where a code starts its execution

Error 3: Putting a 0 value for non-existing variables

❌ Wrong: Putting a 0 value in the column when the variable has not yet been created at a given step of the code.

✅ Correct: if at a step of the code a variable has not yet been created, then leave the column blank or put a – in it.


💡 Tracing Tips for Success

  1. Be systematic: Don’t skip lines or take mental shortcuts
  2. Show your work: Write intermediate calculations
  3. Use clear symbols: Use — for uninitialized variables
  4. Check your answer: After tracing, if possible, run the code to verify

Remember: Tracing is like being a human computer. Follow the instructions exactly as Python would, and you’ll understand exactly what your code does!