Phase 1: Prepare
Step 1 Warm-up recall
From Module 2: what does an if statement need after its condition?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- write a for loop over range (Foundation);
- list the values range produces (Core);
- count how many times a loop runs (Core).
Phase 2: Teach
Step 3 A familiar example
A teacher who writes a name on twenty exercise books does the same action twenty times. A for loop is the instruction: for each book, write the name.
Step 4 Clear explanation
A for loop repeats its indented lines once for each value in a sequence. The loop variable takes the next value on each pass.
for i in range(3):
print("Hello", i)
print("Done")Hello 0
Hello 1
Hello 2
Donerange makes the sequence of numbers. It counts from 0 by default and never reaches the stop value.
| Call | Values produced | How many |
|---|---|---|
| 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(3, 3) | none | 0 |
A for loop also works on text, one character at a time: for ch in "abc": runs three times. The line under the loop that is not indented, such as print("Done"), runs once after the loop ends.
Step 5 Worked example (4 marks)
How many times does for k in range(2, 7): run, and what are the values?
- range(2, 7) starts at 2 and stops before 7. (1 mark)
- Values: 2, 3, 4, 5, 6. (1 mark)
- Count them: 5. (1 mark)
- Answer: 5 times, with k from 2 to 6. (1 mark)
Go deeper: the professor's notes
From range.
Indented.
Runs once.
range
| Call | Values |
|---|---|
| range(3) | 0, 1, 2 |
| range(1, 4) | 1, 2, 3 |
At work: A repeated switch
A switch must be checked 5 times.
- Use range(5).
- Place the check in the block.
- Test the first and last pass.
What a careful practitioner decides. State the block and exactly what ends it.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. range(4) produces:
It starts at 0 and stops before 4.
2. range(1, 6) runs a loop:
Values 1 to 5.
3. The values of range(0, 10, 2) are:
The third number is the step.
4. A line after the loop that is not indented runs:
Indentation marks the loop body.
Phase 4: Close
Step 7 Recap
- A for loop repeats its indented block for each value.
- range(start, stop, step) never reaches stop.
- Count values as stop minus start when the step is 1.
Step 8 Exit ticket (ungraded)
List the values of range(3, 8) and say how many times a loop over it runs.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a loop that prints the five times table from 5 to 50.
In industry: AT&T, 15 January 1990
On 15 January 1990 a software fault in AT&T's long-distance switches caused about half of the calls on its network to fail. Accounts describe a break statement in a loop that ended the wrong block of code, and about 70 million calls failed over nine hours.
Link to this lesson. A loop is a block that repeats. What belongs inside it, and what ends it, must be exact. Python makes the block visible by indentation.
Source: The New York Times, 16 Jan 1990; cs4fn, The AT&T crash; Sterling, The Hacker Crackdown
Common mistakes
| Mistake | Why it fails |
|---|---|
| Expecting range(5) to include 5 | It stops at 4. |
| Forgetting the colon | The for line ends with a colon. |
| Indenting the line meant to run once | Only the repeated lines are indented. |
Limits. This lesson uses range and text; other sequences appear in Module 4.