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.
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)
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
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)
Mistakes Beginners Make
Assuming that the stop value is included.
The range sequence goes up to but does not include its stop value.
Fix:
Trace the generated values as 1, 2, and 3.Treating the loop variable as the whole sequence.
The loop variable receives one current item at a time.
Fix:
Track its value separately for each iteration.Forgetting that the loop body repeats.
The loop body runs for each item in the sequence.
Fix:
Count the sequence items and trace one body execution for each.Using the wrong step direction.
The step controls how the generated sequence changes, including whether it counts by different increments or backwards.
Fix:
Choose 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.
- Identify the sequence that should be visited.
- Choose a loop variable name for the current item.
- Write the loop body as the action that should occur once per item.
- Trace 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
- A for loop repeats its body once for each item in a sequence.
- The loop variable receives one current item at a time.
- range(start, stop) begins at start, excludes stop, and uses a step of 1 by default.
- The third range() parameter controls the step, allowing different increments or backwards counting.
- Tracing the sequence of loop-variable values makes the loop's output predictable.
Key Takeaways
- 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.