Unit 05 · lesson

Repeating With `for`

Repeating With for

Core path: 25 minutes

How can Python repeat something a known number of times?

Instead of writing the same instruction manually, describe the repetition once:

for number in range(5):
    print(number)

Output:

0
1
2
3
4
Interactive model

Follow one for-loop execution cycle

The loop receives a value, runs the body, advances to the next value, and stops only when the iterable is exhausted.

Drag nodes to inspect the relationships. Motion shows the active path; the plain background keeps attention on the relationships instead of graph-paper decoration.

View static diagramStatic Python for loop execution cycle

Understanding range()

range(5) begins at 0 and stops before 5.

Range Visualizer
Range Visualizer

Diagrams open at a readable shape-aware scale. Zoom or expand when you need more detail.

A common failure is assuming the stop value is included. That creates an off-by-one error: the program runs one fewer or one more iteration than the developer intended.

Use the runnable comparison below to inspect several range() shapes side by side:

range_boundaries.py
OutputRun with button or Ctrl/Cmd+Enter
Run the code to see output.
Ready to edit. Press Run when you want evidence.

Predict every sequence before running. Then change one stop value by 1 and identify the exact off-by-one effect.

Do not just read the lists after running. Predict each sequence first, then change exactly one stop value and identify the one boundary effect that changed.

Loop anatomy

for number in range(5):
│      │         │
│      │         └── sequence of values
│      └── loop variable
└── repeat

The indented block is the loop body. It executes once for each value.

Trace the loop

for number in range(3):
    print(f"Number: {number}")

Trace:

iteration 1 → number = 0 → Number: 0
iteration 2 → number = 1 → Number: 1
iteration 3 → number = 2 → Number: 2
stop

Start, stop, and step

for number in range(1, 6):
    print(number)

prints 1 through 5 because 6 is excluded.

for number in range(0, 11, 2):
    print(number)

uses range(start, stop, step).

Guided example: choose the range from the required output

Requirement:

Scan 1
Scan 2
Scan 3

One correct loop is:

for scan_number in range(1, 4):
    print(f"Scan {scan_number}")

Work backward from the evidence. The first required value is 1. The final required value is 3, so the excluded stop must be 4.

Activity: Predict Before Running

Predict, run, and explain:

for x in range(2, 8):
    print(x)

for x in range(2, 8, 2):
    print(x)

for x in range(10, 0, -2):
    print(x)

Vocabulary lab

Flip the idea, not just the card

Explain the term before you reveal the back. Then compare your explanation with the definition, example, and warning.

1 / 5
Read all terms without animation
Loop
A programming structure that repeats a block of instructions. Example: A for loop repeating a scan five times. Do not confuse it with: A conditional branch that chooses one path once.
Iteration
One execution of a loop body. Example: A loop that runs five times performs five iterations. Do not confuse it with: The loop statement as a whole.
range()
A Python function that represents a sequence of integer values commonly used for iteration. Example: range(1, 6) produces 1 through 5 when iterated. Do not confuse it with: A stored list that must contain all values in memory.
Loop Variable
The name that receives the current item during each iteration. Example: number in 'for number in range(5)'. Do not confuse it with: A value that never changes during the loop.
Off-by-One Error
A logic error where a boundary causes one extra or one missing iteration or item. Example: Expecting range(5) to include the value 5. Do not confuse it with: A syntax error that prevents the program from running.