01 / 20
0 5 10

Python range()

Master the start, stop, and step parameters to generate number sequences and eliminate off-by-one errors forever.

Python

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.

# Quick taste for i in range(5): print(i, end=" ")
Output
0 1 2 3 4

Why range() Matters

For Loops
The primary way to repeat code a specific number of times in Python.
Index Access
Generate indices to traverse lists, strings, and other sequences by position.
Memory Efficient
Produces numbers lazily: 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:

1
range(stop): generates \(0, 1, 2, \ldots, \text{stop} - 1\)
2
range(start, stop): generates \(\text{start}, \text{start}+1, \ldots, \text{stop} - 1\)
3
range(start, stop, step): generates \(\text{start}, \text{start}+\text{step}, \text{start}+2\cdot\text{step}, \ldots\) while < stop

range(5) on a Number Line

0 1 2 3 4 5 start=0 stop=5 ✓ included: 0, 1, 2, 3, 4 ✕ excluded: 5

range(2, 7) on a Number Line

0 1 2 3 4 5 6 7 start=2 stop=7 Output: 2, 3, 4, 5, 6
list(range(2, 7)) # [2, 3, 4, 5, 6]

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.

0 1 2 3 4 5 6 7 8 9 10 +2 +2 +2 +2 range(0, 10, 2) → 0, 2, 4, 6, 8
list(range(0, 10, 2)) # [0, 2, 4, 6, 8]

Counting Backwards

A negative step counts downward. The start must be greater than stop, and the stop value is still excluded.

0 1 2 3 4 5 6 7 8 9 10 -2 -2 -2 -2 range(10, 0, -2) → 10, 8, 6, 4, 2
list(range(10, 0, -2)) # [10, 8, 6, 4, 2]

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:

Length Formula
The number of elements in range(start, stop) is exactly \(\text{stop} - \text{start}\).
Last Value
The last element produced is always \(\text{stop} - 1\) (for step = 1).

How to Apply range()

1
Identify the first value you need. That becomes start (default 0).
2
Identify the last value you need, then add 1. That becomes stop.
3
Check the increment. If it's not 1, set step explicitly. Going backwards? Use a negative step.
4
Verify: wrap in list() to sanity-check during development.
5
Count check: confirm the length matches expectations. For step=1: \(\text{stop} - \text{start}\). For any step: \(\lceil (\text{stop} - \text{start}) / \text{step} \rceil\).

Example 1: Print 1 to 10

Goal: Print every integer from 1 through 10 inclusive.

# Step 1: start = 1 # Step 2: last value = 10, so stop = 10 + 1 = 11 # Step 3: increment = 1 (default) for i in range(1, 11): print(i, end=" ")
Output
1 2 3 4 5 6 7 8 9 10

Example 2: Even Numbers 0 to 20

Goal: Generate all even numbers from 0 to 20 inclusive.

# start = 0, last value = 20 → stop = 21, step = 2 evens = list(range(0, 21, 2)) print(evens)
Output
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Example 3: Countdown Timer

Goal: Countdown from 5 to 1, then print "Go!"

# start = 5, last value = 1 → stop = 0, step = -1 for i in range(5, 0, -1): print(i, end="... ") print("Go!")
Output
5... 4... 3... 2... 1... Go!

Example 4: Index a List

Goal: Print each element of a list with its index number.

colors = ["red", "green", "blue", "yellow"] for i in range(len(colors)): print(f"Index {i}: {colors[i]}")
Output
Index 0: red Index 1: green Index 2: blue Index 3: yellow

Common Mistakes

✕ Forgetting stop is excluded
range(1, 10) gives 1..9, not 1..10. Always add 1 to include the endpoint.
✕ Wrong direction with step
range(10, 0, 1) is empty: a positive step can't reach below the start. Use -1 instead.
✕ Using step = 0
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?

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?

Question 5 of 5

What happens when you call range(5, 5)?