Coding learning skills
How to trace a Python for loop by hand
Create one row per iteration, update variables in execution order, record output exactly, and compare your prediction with Python only after the trace is complete.
Published

Start with the iterable and the loop variable
A Python for loop takes successive items from an iterable. Before tracing the body, write the items in the order Python will use them. For range(1, 5), those values are 1, 2, 3 and 4. The stop value is excluded.
Next, identify the loop variable. In for number in range(1, 5), number changes at the start of each iteration. Variables created before the loop keep their previous value until a line changes them.
The opening coding photograph is AI-generated with fictional Indonesian people. It does not show a real tutor, learner, school or client session.
Build a table before calculating
Give the trace table a column for the iteration number, the current item, every variable that changes, the result of any condition and anything printed. Record the state after each line that matters rather than trying to hold the whole program in memory.
Suppose total starts at 0 and the loop adds each number from range(1, 5). The table makes the accumulation visible and exposes an incorrect assumption about the final value.
| Iteration | number | total after addition | printed value |
|---|---|---|---|
| Start | Not assigned | 0 | Nothing |
| 1 | 1 | 1 | 1 |
| 2 | 2 | 3 | 3 |
| 3 | 3 | 6 | 6 |
| 4 | 4 | 10 | 10 |
Follow indentation and execution order
Python uses indentation to define the loop body. Read the indented lines from top to bottom for the current item, then return to the for statement for the next item. A line aligned with the for statement runs only after the loop finishes.
When an if statement appears inside the loop, evaluate it with the current row’s values. Record True or False before following the matching branch. This prevents a later value from leaking backwards into an earlier decision.
Treat break and continue as control-flow changes
A continue statement skips the remaining lines of the current iteration and starts the next one. A break statement ends the loop completely. Mark the jump in the table rather than filling cells for lines that never run.
Nested loops need separate current-item columns. Change the inner loop value through all of its items before advancing the outer loop. If the table becomes confusing, trace just the first outer iteration, verify the pattern, and then continue.
Predict first, then run the code

This coding image is AI-generated with fictional people and does not document an actual lesson.
Complete the trace without executing the program. Then run the exact code and compare every printed value. A mismatch is useful: locate the first row where the prediction differs, then check range endpoints, indentation, update order and the condition.
Change one input after the first trace. Predicting how the table will change is stronger evidence of understanding than copying a result from the console.
Get help understanding Python control flow
If a learner can read individual lines but loses track across a loop, bring the short program and the attempted trace to a coding tutoring discussion. The commercial coding page owns lesson enquiries; this article provides a debugging routine.
Families in Berawa can review the Berawa tutoring area page. The same table method also supports while loops, conditionals and early debugging of school computing tasks.
Frequently asked questions
Does range(1, 5) include 5?
No. It produces 1, 2, 3 and 4 because Python excludes the stop value.
When should I record a variable in a trace table?
Record its initial value and every change that occurs during each executed iteration. Leave skipped lines unrecorded when control flow bypasses them.
Related
Message us on WhatsApp: +62 858 6969 6869 · info@privatetutoringbali.com · We reply within one working day.