Unit 11.1A · Term 1

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.

range(2, 8, 2)
0
1
2start
3
4
5
6
7
8stop
2 → 4 → 6   |   stop 8 is excluded

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.

Expression

                                    Generated values
                                    

                                    Summary
                                    

                                

What does range() produce?

Write the sequence before revealing.

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 values
→
1 → 2 → 3 → 4one value at a time
→
i → bodyPython assigns each next value to i

For Loop Tracer

for i in range(2, 10, 2): print(i)
Iteration i Output
Press Next iteration to begin.

Common for-Loop Patterns

Choose a for-loop pattern:

Accumulator Visual

i = 1total = 1
i = 2total = 3
i = 3total = 6
i = 4total = 10
i = 5total = 15

for 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

Choose an example:

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

Print 1 to 10 inclusiveStop must be one beyond 10.range(1, 11)
Print even numbers 2 to 20 inclusiveKeep step 2 and place stop beyond 20.range(2, 21, 2)
Print 10 to 1 inclusiveA negative step requires a lower excluded stop.range(10, 0, -1)

Exam strategy — do not rely on one formula

  1. Identify the start value.
  2. Remember that stop is excluded.
  3. Follow the sign and size of the step.
  4. Write the generated sequence when unsure.
  5. Count the values in that sequence.

Range Count Trainer

range(2, 100, 7)
First: 2
Second: 9
Last: ?
Iterations: ?
Work out the final value and number of generated values first.

Range Error Lab

Decide what happens.

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

Remember

Write the values generated by range(3, 15, 3) and range(10, 0, -2).

Understand

Explain why range(1, 10) does not include 10 and write a range that does include it.

Understand

Explain why range(5, 2) is empty. Give a range that counts from 5 down to 3.

Apply

Write a program that calculates the factorial of N using a for loop.

Apply

Print all odd numbers from 1 to 49 using the step argument of range().

Apply

Write a for loop that prints 20, 15, 10 and 5.

Analyze

How many times does for i in range(2, 100, 7) execute? List the first five and last two values of i.

Create

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
2 4 6 8

Q2: State the output. [2]

total = 0 for i in range(1, 5): total += i print(total)
Reveal marking points
10 range(1, 5) supplies 1, 2, 3, 4. 1 + 2 + 3 + 4 = 10.

Q3: Write a for loop that outputs 20, 15, 10, 5. [2]

Reveal marking points
for i in range(20, 0, -5): print(i)

Q4: Explain why range(2, 10, -2) produces no values. [2]

Reveal marking points
The start value 2 is below stop 10, but the step is negative. The sequence moves away from the stop boundary, so the range is empty.

Self-Check Quiz

Eight quick checksClick each answer to reveal
1. What values does range(5) generate?
Click to reveal answer
2. What does range(2, 8, 2) produce?
Click to reveal answer
3. How do you count from 10 down to 1?
Click to reveal answer
4. Is the stop value normally included?
Click to reveal answer
5. What does range(5, 2) produce?
Click to reveal answer
6. What happens with range(1, 10, 0)?
Click to reveal answer
7. Can standard range() use float arguments?
Click to reveal answer
8. What does break do inside a for loop?
Click to reveal answer