- Understand what a fixed (counted) loop is and why programs use them
- Use
forandrange()to repeat instructions a set number of times - Use the loop variable inside a loop
- I can write a
forloop usingrange() - I can explain how many times a given
range()loop will run - I can use a fixed loop to build a running total
Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.
1. If you were told "clap your hands 5 times," how many claps would that be?
2. Which of these is an example of repeating something a fixed number of times?
3. True or false: a fixed loop is useful when you already know exactly how many times something should repeat.
Key vocabulary
range().for loop to repeat over.for loop that holds the current value from the sequence on each pass.Fixed Loops (for)
What is a fixed loop?
for day in range(1, 6)The stop value is not included, so range(1, 6) gives five loop values.
Iteration is the third of the three basic programming constructs (alongside sequence and selection) and means repeating a block of instructions. A fixed loop (also called a counted loop) repeats a known, set number of times, decided before the loop even starts. This is different from a conditional loop (covered next lesson), which repeats until something becomes true or false, without necessarily knowing the number of repeats in advance.
The for loop and range()
In Python, a fixed loop is written using for together with range(). range(5) produces the sequence 0, 1, 2, 3, 4 — five numbers, starting at 0. This is a common trip-up: range(5) runs the loop 5 times, but the last value it produces is 4, not 5, because counting starts from zero.
| Code | Produces | Number of repeats |
|---|---|---|
range(5) | 0, 1, 2, 3, 4 | 5 |
range(1, 6) | 1, 2, 3, 4, 5 | 5 |
range(0, 10, 2) | 0, 2, 4, 6, 8 | 5 |
range(start, stop) begins at start and stops just before stop — the stop value itself is never included. Adding a third number, range(start, stop, step), changes how much the sequence increases by each time, instead of the default of 1.
Using the loop variable
The structure of a fixed loop is for i in range(5): followed by an indented block, exactly like selection. i is the loop variable — it takes each value from range() in turn, and can be used inside the loop, for example to print it or use it in a calculation. The name i is just a convention, not a rule — any valid variable name works.
Building a running total
A very common use of a fixed loop is to build up a running total — a variable that accumulates a value across every pass of the loop. This requires setting the total to 0 before the loop starts, then adding to it once per pass, inside the loop.
Worked examples
A program prints "Hello" three times.
for i in range(3):print("Hello")Hello three separate times, once for each value i takes (0, 1, 2) — note i is never actually used here, and that's fine.A program prints the numbers 1 to 5.
for number in range(1, 6):print(number)1, 2, 3, 4, 5 on separate lines. range(1, 6) is used, not range(1, 5), because 6 is never reached — it stops just before it.A program adds up the numbers 1 to 5.
total = 0 — set up before the loop starts.for number in range(1, 6):total = total + numberprint(total) outputs 15 (1+2+3+4+5), once the loop has finished all five passes.A program contains: total = 0, then for number in range(2, 9, 2): with total = total + number indented underneath, then print(total) after the loop.
Answer the following:
- What values does
numbertake across the loop? - How many times does the loop run?
- What value does
totalhold when the program finishes?
numbertakes the values 2, 4, 6, 8 (starting at 2, stepping by 2, stopping before 9).- The loop runs 4 times.
totalholds20(2+4+6+8).
range(5) counts to 5. It actually produces 0, 1, 2, 3, 4 — five values, but stopping one short of 5.total = 0 inside the loop resets it to zero every single pass, so it never accumulates. It must be set once, before the loop starts.range() stops before its second argument. range(1, 5) produces 1, 2, 3, 4 — not up to and including 5.N4 pupils: when explaining a for loop, state clearly how many times it repeats and why — for example "this loop runs 5 times because range(5) produces five values, 0 to 4" — rather than just describing what happens inside it.
Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.
1. How many times does for i in range(7): repeat? TYPE 1
2. What is the last value produced by range(1, 5)? TYPE 1
3. What values does range(0, 10, 5) produce? TYPE 1
4. Why must total = 0 be written before a running-total loop, not inside it? TYPE 1
5. What does the loop variable in for number in range(3, 6): take across the loop? TYPE 1
6. Explain, using an example, why range(5) produces the numbers 0 to 4 rather than 1 to 5. TYPE 2 N4
range(5) means "produce 5 numbers starting from 0," which gives 0, 1, 2, 3, 4 — five values in total, but stopping one short of 5 itself. This is different from everyday counting, which usually starts from 1.7. Explain the purpose of a running total, and why it must be set up before the loop starts. TYPE 2 N3
8. Practical: In IDLE, create a new file called sdd4_loops.py. Write a program using a for loop that prints the numbers 1 to 10. Test it and record your output below. TYPE 3
for number in range(1, 11):
print(number)Expected output: the numbers 1 to 10, each on its own line.
9. Practical: In sdd4_loops.py, extend your program to also calculate and print the running total of the numbers 1 to 10. Record your output. TYPE 3
total = 0
for number in range(1, 11):
total = total + number
print(total)Expected output:
55 (1+2+...+10), printed after the numbers from Q8.Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.
Key misconception to address: zero-indexing of range() — pupils very reliably expect range(5) to include 5. Trace through it on the board with a tally.
Live demo suggestion: live-code the running total example, then deliberately move total = 0 inside the loop and run it, so pupils see the bug happen rather than just being told about it.
Extension idea (S5 / N5-bridging, non-assessed): briefly show looping over a list directly (for item in [1, 2, 3]:) as a "look ahead" — keep clearly separate from the assessed task set.
Assessment standards covered: N3 O2.1; N4 O1.2, O2.1.