Python range()
Master the start, stop, and step parameters to generate number sequences and eliminate off-by-one errors forever.
What is range()?
Python's range() is a built-in function that produces an immutable sequence of integers. It doesn't create a list in memory: it generates each number on demand, making it extremely memory-efficient.
Why range() Matters
range(10**9) uses almost no RAM.The Three Signatures
range() accepts one, two, or three integer arguments. Each form builds on the previous one:
range(stop): generates \(0, 1, 2, \ldots, \text{stop} - 1\)range(start, stop): generates \(\text{start}, \text{start}+1, \ldots, \text{stop} - 1\)range(start, stop, step): generates \(\text{start}, \text{start}+\text{step}, \text{start}+2\cdot\text{step}, \ldots\) while < stoprange(5) on a Number Line
range(2, 7) on a Number Line
The Step Parameter
The third argument controls the increment between consecutive values. It can be positive (count up) or negative (count down), but never zero.
Counting Backwards
A negative step counts downward. The start must be greater than stop, and the stop value is still excluded.
range() vs list
| range() | list | |
|---|---|---|
| Type | Lazy sequence (generator-like) | Eager collection in memory |
| Memory | Constant: only stores start, stop, step | Linear: stores every element |
| Indexing | ✓ range(10)[3] |
✓ [0,1,...][3] |
| Mutability | Immutable | Mutable |
| Convert | list(range(5)) → [0, 1, 2, 3, 4] |
|
The Off-By-One Rule
The most common range() bug is the off-by-one error: getting one too few or one too many values. Memorize these two formulas:
range(start, stop) is exactly \(\text{stop} - \text{start}\).How to Apply range()
start (default 0).stop.step explicitly. Going backwards? Use a negative step.list() to sanity-check during development.Example 1: Print 1 to 10
Goal: Print every integer from 1 through 10 inclusive.
Example 2: Even Numbers 0 to 20
Goal: Generate all even numbers from 0 to 20 inclusive.
Example 3: Countdown Timer
Goal: Countdown from 5 to 1, then print "Go!"
Example 4: Index a List
Goal: Print each element of a list with its index number.
Common Mistakes
range(1, 10) gives 1..9, not 1..10. Always add 1 to include the endpoint.range(10, 0, 1) is empty: a positive step can't reach below the start. Use -1 instead.range(0, 5, 0) raises ValueError. A step of zero would loop forever, so Python rejects it.Question 1 of 5
What does list(range(3)) produce?
range(3) starts at 0 by default and stops before 3, producing 0, 1, 2. That's 3 elements total: \(3 - 0 = 3\).Question 2 of 5
How many integers does range(4, 10) produce?
Question 3 of 5
What is the output of list(range(1, 10, 3))?
Question 4 of 5
Which call produces the sequence 5, 4, 3, 2, 1?
range(5, 0, -1) starts at 5, steps by -1, and stops before 0. That gives exactly 5, 4, 3, 2, 1. Options A and B omit the step so they default to +1 and produce empty sequences.Question 5 of 5
What happens when you call range(5, 5)?
start == stop, there are no values to generate: \(5 - 5 = 0\) elements. It's not an error: Python simply returns an empty range.