Module 6 · Loops and Iteration — topic 4 of 12
range()
range() produces sequences of integers for counting loops. Learn its start, stop and step arguments, counting backwards, why the stop value is excluded, and how range saves memory.
Learning objectives
- Use range() with one, two and three arguments
- Count backwards with a negative step
- Explain why the stop value is excluded
Definition
range() produces sequences of integers for counting loops. Learn its start, stop and step arguments, counting backwards, why the stop value is excluded, and how range saves memory.
Introduction
range() generates a sequence of whole numbers. Combined with for, it is how you repeat something a fixed number of times or loop over numeric positions. range(5) gives 0, 1, 2, 3, 4.
Why it matters
Counting loops, tables, timetables and index-based processing all need sequences of numbers. range() produces them without building a list, so even range(1_000_000_000) costs almost no memory.
Detailed explanation
range(stop): from 0 up to but not including stop.
range(start, stop): from start up to but not including stop.
range(start, stop, step): moves by step each time; step can be negative to count down, but never zero.
The stop value is excluded so that range(n) has exactly n numbers and range(0, len(items)) matches every valid index.
If the start is already past the stop in the direction of the step, the range is empty, and the loop body never runs.
range is lazy: it stores only start, stop and step and calculates numbers on demand. list(range(5)) shows its numbers when you need to see them.
Ranges support len(), indexing and in checks: 7 in range(0, 10, 2) is False.
Only integers are accepted. range(0, 1, 0.1) raises TypeError; for decimal steps, loop over integers and divide, for example for i in range(11): print(i / 10). This also avoids accumulating floating-point error from repeated addition.
A handy way to remember the arguments: range(start, stop, step) describes where to begin, the first value not to reach, and how far each step jumps.
Syntax
- All arguments must be integers.
- stop is never included.
- A negative step counts downwards.
Code examples
The three forms
[0, 1, 2, 3, 4] [2, 3, 4, 5, 6] [1, 4, 7]
Counting down and empty ranges
[10, 8, 6, 4, 2] [] 20
More examples
A loan schedule with range
Month 1: paid 10000, remaining 50000 Month 2: paid 10000, remaining 40000 Month 3: paid 10000, remaining 30000 Month 4: paid 10000, remaining 20000 Month 5: paid 10000, remaining 10000 Month 6: paid 10000, remaining 0 Even months: [2, 4, 6]
Step-by-step explanation
- range(1, months + 1) gives 1 to 6; the + 1 includes the last month.
- Each iteration subtracts one instalment from the principal.
- The f-string reports the month, payment and remaining balance.
- range(2, months + 1, 2) picks every second month.
Expected output
Running “A loan schedule with range” prints exactly:
Practical applications
- Timetable generators loop over periods with range.
- Pagination code loops over page numbers.
- Games run a fixed number of rounds with for round_number in range(1, rounds + 1).
Best practices
- Use range(1, n + 1) when counting from 1 to n.
- Prefer direct iteration over range(len(...)).
- Use named variables for start, stop and step in non-trivial loops.
Common mistakes
Off-by-one with stop
Avoid
Prefer
The stop value is excluded, so add one to include the last number.
Using range(len()) when items are enough
Avoid
Prefer
Looping over items directly is clearer; use enumerate if you also need the index.
Key takeaways
- range(start, stop, step) produces integers, excluding stop.
- A negative step counts down; a zero step is an error.
- range is lazy and memory-efficient.
Try it yourself
- 1Print all odd numbers from 1 to 21 using range with a step.
- 2Print a countdown from 10 to 0 using range.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
2 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?