Read Python Like a Pro / Reading code
Lesson 2 of 10
Tracing variables
Tracing means following each variable as the program runs. Write the variables in a little table on paper and update them line by line. It's the single best way to understand loops.
Key points
- One row per line that runs
- Cross out old values, write the new one
- Loops repeat the same rows
total = 0
for n in [2, 4, 6]:
total = total + n
print(total)Your turn
Trace this on paper: x = 1, then a loop that runs 4 times doing x = x * 2. What is x at the end? Check by running it.
Quiz · +10 XP
What is total after the loop finishes?
Log in to save progress and earn XP.