for Loops
A for loop repeats a block of code for each item in a sequence, assigning each item to a loop variable one at a time.
Try it: Loop Tracer
How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.
How it works
- Initialise the accumulator before the loop.
- Each iteration, the loop variable takes the next value from the list.
- An if inside the loop decides whether to update the accumulator.
- After the last item the loop ends and the accumulator holds the answer.
Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.
Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.
Loading the simulation…
A Sequence of Small Steps
A for loop is useful when the same block of code must be repeated for each item in a sequence. Instead of treating the repetition as one large action, the loop visits one item, assigns that item to a loop variable, runs the loop body, and then moves to the next item. This continues until the sequence has no items left.
Suppose a task requires processing every item in a known sequence. A for loop matches that task because its basic operation is to take one item at a time and repeat the same block for each item.
The Loop Variable Moves
The loop variable is the name that receives the current item. On the first iteration, it receives the first item in the sequence. After the loop body finishes, it receives the next item, and the body runs again. The loop variable therefore represents the item currently being processed, not the entire sequence at once.
red
blue
greenReading the for...in Pattern
A for loop repeats a block of code for each item in a sequence, assigning each item to a loop variable one at a time.
for item in sequence:
process(item)- 1The loop obtains the next item from the sequence.
- 2That item is assigned to item.
- 3The indented loop body runs using the current value.
- 4The loop returns to obtain the next item and repeats until the sequence is exhausted.
- for begins the iteration statement.
- item is the loop variable that receives the current sequence item.
- in connects the loop variable to the sequence being visited.
- sequence supplies the items one at a time.
process(item)is the loop body and runs for each item.
Generating Number Sequences
The range() function generates a sequence of numbers. With range(start, stop), the sequence begins at start and continues up to, but does not include, stop. If no step is supplied, the sequence increases by 1. A third parameter controls the step size, so a range can count by larger increments or count backwards.
2
4
6Tracing Each Iteration
What do you think happens?
What output is produced, and what value does number have during each iteration?
Reveal answer
The output is 1, 2, 3, with each value printed on its own line.
The loop variable receives 1 first, then 2, then 3. The stop value 4 is excluded.
for number in range(1, 4):
print(number)- 1First iteration: number is 1, so the loop body prints 1.
- 2Second iteration: number is 2, so the loop body prints 2.
- 3Third iteration: number is 3, so the loop body prints 3.
- 4The sequence has no further values before 4, so the loop ends.
range(1, 4)generates the values 1, 2, and 3.- number receives 1 during the first iteration, 2 during the second, and 3 during the third.
print(number)runs once for each value.
Mistakes Beginners Make
Common mistake Assuming that the stop value is included.
What it looks likeTreating range(1, 4) as the values 1, 2, 3, and 4.Why it failsThe range sequence goes up to but does not include its stop value.
Do this insteadTrace the generated values as 1, 2, and 3.
Common mistake Treating the loop variable as the whole sequence.
What it looks likeThinking number represents every value in range(1, 4) during every iteration.
Why it failsThe loop variable receives one current item at a time.
Do this insteadTrack its value separately for each iteration.
Common mistake Forgetting that the loop body repeats.
What it looks likeExpecting print(number) to run only once in a loop over several numbers.
Why it failsThe loop body runs for each item in the sequence.
Do this insteadCount the sequence items and trace one body execution for each.
Common mistake Using the wrong step direction.
What it looks likeExpecting a positive step to count backwards.
Why it failsThe step controls how the generated sequence changes, including whether it counts by different increments or backwards.
Do this insteadChoose a step that matches the intended direction and increment.
Choosing the Structure
Choose a for loop when the task requires the same processing for each item in a known sequence. The sequence may provide its items directly, or range() may generate a sequence of numbers with the required start, stop, and step. Before writing the loop, identify the sequence, the loop variable, and the action that should happen once per item.
- 1Identify the sequence that should be visited.
- 2Choose a loop variable name for the current item.
- 3Write the loop body as the action that should occur once per item.
- 4Trace the first item, the next item, and the final item to check the expected result.
Trace this loop before checking the result. List the value of step during each iteration and the output produced:
for step in range(3, 10, 3):
print(step)Hints
- Begin at the start value, 3.
- Add the step value, 3, after each iteration.
- Stop before reaching the stop value, 10.
Key Takeaways
- 1A for loop repeats its body once for each item in a sequence.
- 2The loop variable receives one current item at a time.
- 3
range(start, stop)begins at start, excludes stop, and uses a step of 1 by default. - 4The third
range()parameter controls the step, allowing different increments or backwards counting. - 5Tracing the sequence of loop-variable values makes the loop's output predictable.
At a glance
- A for loop processes a sequence one item at a time.
- Each iteration assigns the current item to the loop variable and runs the loop body.
range()creates number sequences using start, stop, and optional step values.- The stop value is excluded from the generated sequence.
- A for loop is appropriate when the same action must be performed for every item in a known sequence.