The range loop: the stop value is excluded
range(start, stop) produces integers from start inclusive to stop exclusive, normally in steps of one. for assigns each value in turn and runs its indented body.
| Observation | What it means |
|---|---|
| range(3) | 0, 1, 2 |
| range(2, 5) | 2, 3, 4 |
One worked example
- s = 0Prepare a total.
- for i in range(1, 4): s = s + iAdd 1, then 2, then 3.
- s = 6The upper bound 4 is not added.
Watch out: A third range argument changes the step; negative steps need suitable bounds. break can end a loop early.
Goal, prerequisites and card origin
What you will learn: Count repetitions of a Python for loop.
What you need first: Integers and assignment.
An original PEAZIT working card. Independent expert review and effectiveness testing have not yet taken place. · v1
Reference for the underlying principle ↗Show connections
range(start, stop) produces integers from start inclusive to stop exclusive, normally in steps of one. for assigns each value in turn and runs its indented body.