The range() Function
range() generates a sequence of numbers. It's the most-used function with for loops. Understand its three forms: range(stop), range(start, stop), range(start, stop, step).
10 min•By Priygop Team•Updated 2026
range() Function
range() Function
# range(stop) — 0 to stop-1
print(list(range(5))) # [0, 1, 2, 3, 4]
# range(start, stop) — start to stop-1
print(list(range(2, 7))) # [2, 3, 4, 5, 6]
# range(start, stop, step) — with step
print(list(range(0, 20, 3))) # [0, 3, 6, 9, 12, 15, 18]
# Negative step — counting down!
print(list(range(10, 0, -1))) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# Common patterns
n = 5
# Even numbers
print("Even:", list(range(0, 20, 2)))
# Odd numbers
print("Odd:", list(range(1, 20, 2)))
# Countdown
for i in range(5, 0, -1):
print(i, end=".. ")
print("Go!")
# Length of a sequence
items = ["a", "b", "c", "d"]
for i in range(len(items)):
print(f"Index {i}: {items[i]}")Tip
Tip
range() is lazy — it generates numbers on demand. range(1_000_000) uses almost no memory. Convert to list only when needed.
Diagram
Loading diagram…
Everything in Python is an object — use type() to check
Common Mistake
Warning
range(5) gives 0-4, not 1-5. For 1-5, use range(1, 6). The stop value is always excluded.
Practice Task
Note
(1) Generate even numbers 0-20. (2) Count down from 10 to 1. (3) Generate multiples of 3 up to 30. (4) Create a list of 5 to 50 in steps of 5.