For Loop & Range
A for loop takes values from an iterable one at a time and
runs the loop body for each value. With range(), Python can generate
predictable integer sequences using a start, stop and
step.
Learning Objectives
- 11.1.2.4 Write program code using a For loop
- 11.1.2.5 Define a range of values for a loop
FOR Loop / Iteration
Craig'n'Dave — count-controlled FOR loops. The FOR-loop section begins at approximately 07:44.
Conceptual Anchor
The Measuring Tape Analogy
range(start, stop, step) is like walking along a measuring tape.
start is where you stand first, step is how far
you move each time, and stop is the boundary you do not include.
Understanding range()
range(2, 10, 3)2start
The first generated value.
10stop — excluded
The sequence stops before reaching this boundary.
3step
The amount added after each generated value.
Result
range(2, 10, 3) generates 2, 5, 8. The next value would be 11,
so the range stops.
| Form | Meaning | Example | Values Generated |
|---|---|---|---|
range(stop) |
Starts at 0, step 1 | range(5) |
0, 1, 2, 3, 4 |
range(start, stop) |
Starts at start, step 1 | range(2, 6) |
2, 3, 4, 5 |
range(start, stop, step) |
Uses the specified integer step | range(0, 10, 2) |
0, 2, 4, 6, 8 |
range(start, stop, step) |
A negative step counts downward | range(5, 0, -1) |
5, 4, 3, 2, 1 |
Critical: stop is excluded
range(1, 5) generates 1, 2, 3, 4. To generate 1 through 5
inclusive, use range(1, 6).
step cannot be zero
range(1, 10, 0) raises ValueError. A zero step could never move
toward a stopping boundary.
Range Builder
Change start, stop and step. The generated sequence and its direction update immediately.
What does range() produce?
Empty Range Detector
range(5, 2)
The default step is +1, but start is already greater than stop. The
sequence moves in the wrong direction → empty.
range(2, 5, -1)
The step is negative, but reaching 5 requires moving upward. Wrong direction → empty.
Anatomy of a for Loop
for i in range(1, 6):
print(i)forIteration keyword
Repeat once for every value supplied by the iterable.
iLoop variable
Receives the next value automatically.
range(1, 6)Iterable
Supplies 1, 2, 3, 4, 5.
print(i)Indented body
Runs once for each supplied value.
range(1, 5)generates values1 → 2 → 3 → 4one value at a timei → bodyPython assigns each next value to iFor Loop Tracer
for i in range(2, 10, 2):
print(i)| Iteration | i | Output |
|---|
Common for-Loop Patterns
Accumulator Visual
i = 1total = 1i = 2total = 3i = 3total = 6i = 4total = 10i = 5total = 15for vs while
| Aspect | for loop | while loop |
|---|---|---|
| Core idea | Iterate over an iterable / generated sequence | Repeat while a condition remains True |
| Common use | Known sequence or predictable repetition | Condition-controlled / unknown repetitions |
| Loop variable | Gets the next iterable value automatically | Usually updated manually |
| Typical example | Process 30 students | Read input until "quit" |
For or While?
Worked Examples
Loop Variable Experiment
for i in range(3):
i = 100
print(i)
Output: 100 100 100. The assignment changes i during the
current iteration.
for i in range(3):
print(i)
i = 100
Output: 0 1 2. At the start of the next iteration, Python assigns the
next value supplied by range().
Precise rule
Assigning a new value to the loop variable affects it during the current iteration, but it does not change the sequence of values supplied by the for loop.
Inclusive End & Range Counting
range(1, 11)range(2, 21, 2)range(10, 0, -1)Exam strategy — do not rely on one formula
- Identify the start value.
- Remember that stop is excluded.
- Follow the sign and size of the step.
- Write the generated sequence when unsure.
- Count the values in that sequence.
Range Count Trainer
range(2, 100, 7)29??Range Error Lab
Pitfalls & Common Errors
Stop value is excluded
range(1, 10) ends at 9. Use range(1, 11) when the requirement
includes 10.
Wrong step direction
range(5, 2) and range(2, 5, -1) are empty because the step
moves away from the stop boundary.
Zero step
range(1, 10, 0) raises ValueError.
range() with floats
range(0.0, 1.0, 0.1) raises TypeError. Standard
range() arguments must be integers.
Modifying the loop variable
Reassigning i affects the current iteration, but the next iteration still
receives the next value from the iterable.
Unnecessary manual update
In a normal for i in range(...) loop, you do not need i += 1.
The next value is supplied automatically.
Graded Tasks
Write the values generated by range(3, 15, 3) and
range(10, 0, -2).
Explain why range(1, 10) does not include 10 and write a range that does
include it.
Explain why range(5, 2) is empty. Give a range that counts from 5 down
to 3.
Write a program that calculates the factorial of N using a for loop.
Print all odd numbers from 1 to 49 using the step argument of
range().
Write a for loop that prints 20, 15, 10 and 5.
How many times does for i in range(2, 100, 7) execute? List the first
five and last two values of i.
Create a program that prints 1, 12, 123,
1234, 12345. Use for loops.
Challenge: Alphabet pattern
Print A, then AB, then ABC, continuing until ABC...XYZ. You may
use chr() and ord().
Exam Practice
Q1: Write the exact output. [2]
for i in range(2, 9, 2):
print(i, end=" ")Reveal marking points
Q2: State the output. [2]
total = 0
for i in range(1, 5):
total += i
print(total)Reveal marking points
Q3: Write a for loop that outputs 20, 15, 10, 5. [2]
Reveal marking points
Q4: Explain why range(2, 10, -2) produces no values.
[2]
Reveal marking points
Self-Check Quiz
range(5) generate?range(2, 8, 2) produce?range(5, 2) produce?range(1, 10, 0)?range() use float arguments?break do inside a for loop?