lesson
A range has a precise stopping point
1 · Learn
A counted loop repeats an action for a defined sequence of values. In Python, range(4) supplies 0, 1, 2 and 3, excluding the stop value 4. The single-line example 'for n in range(4): print(n)' prints those four values in order.
Count iterations separately from the final value. A loop starting at zero can run four times even though its last value is three. In multi-line Python code, indentation identifies the loop body.
2 · Worked example
For range(2,5), the values are 2, 3 and 4. Changing the stop to 6 adds one more value, not an extra copy of every earlier value.
3 · Your turn
List the values of range(1,4). How many iterations occur?
Check your answer
1, 2 and 3; three iterations.
4 · Apply your learning
Predict and run three small range examples, recording starting value, stopping rule and iteration count.
Adult guidance and safety
Use an approved private interpreter, simulator or authoring tool with fictional data. Trace and predict before running. Never execute unknown downloaded code, inspect real credentials or use other people's private data. Working examples still need saved-project and assessment integration before release.
Practice this lesson
Lesson resources
Browse the available practice activities and worksheets for this lesson.