range()
range(stop)
range(start, stop)
range(start, stop, step) range object · Updated March 13, 2026 · Built-in Functions The range() function generates an immutable sequence of integers that is commonly used for looping a specific number of times in for loops. Despite its name, range() is actually a built-in sequence type in Python, not a function. It behaves like a lazy list — it doesn’t store all values in memory but generates them on demand, making it memory-efficient for large sequences.
Syntax
range(stop)
range(start, stop)
range(start, stop, step)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
start | int | 0 (if stop provided) | The starting integer of the sequence. Included in the result. |
stop | int | — | The end of the sequence. This value is not included in the result. |
step | int | 1 | The difference between each number in the sequence. Can be positive or negative, but cannot be zero. |
Returns: A range object, which is an immutable sequence type. Convert to a list with list() if you need all values at once, or iterate directly with a for loop.
Raises: ValueError if step is zero.
Examples
Basic usage with stop only
When you provide a single argument, it becomes the stop value, and start defaults to 0:
for i in range(5):
print(i)
# 0
# 1
# 2
# 3
# 4
Notice that the sequence stops at 4, not 5 — the stop value is exclusive.
Using start and stop
Provide two arguments to specify both the start and end of the sequence:
for i in range(2, 8):
print(i)
# 2
# 3
# 4
# 5
# 6
# 7
Using step to control increments
The step parameter controls the difference between each number:
for i in range(0, 10, 2):
print(i)
# 0
# 2
# 4
# 6
# 8
Negative step for reverse sequences
Use a negative step to generate sequences in reverse order:
for i in range(5, 0, -1):
print(i)
# 5
# 4
# 3
# 2
# 1
Converting to a list
Since range() returns a lazy iterator, convert to a list when you need all values:
numbers = list(range(10))
print(numbers)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Common Patterns
Looping a specific number of times
The most common use case is repeating an action a fixed number of times:
for i in range(3):
print("Hello!")
# Hello!
# Hello!
# Hello!
Iterating with index
Combine range() with len() to get both index and value when iterating:
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"{i}: {fruits[i]}")
# 0: apple
# 1: banana
# 2: cherry
Note: In modern Python, enumerate() is often preferred for this pattern as it’s more readable.
Creating padded sequences
Use range() to generate indices for padding or alignment:
# Create a list of zeros with a specific length
zeros = [0] * 5 # This creates [0, 0, 0, 0, 0]
# Or for padding values
indexed_values = [(i, 0) for i in range(5)]
print(indexed_values)
# [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0)]
Skipping elements in a sequence
Use step values greater than 1 to process every nth item:
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# Process every 3rd element
for i in range(0, len(data), 3):
print(f"Index {i}: {data[i]}")
# Index 0: 10
# Index 3: 40
# Index 6: 70
# Index 9: 100
Checking membership efficiently
Since range implements the __contains__ method, checking membership is fast:
if 5 in range(10):
print("Found it!")
# Found it!
# This is O(1) — much faster than checking a list
Behavior Notes
Empty sequences
If the range would contain no values, you get an empty sequence:
print(list(range(5, 5)))
# []
print(list(range(10, 0, 1)))
# []
Step cannot be zero
Passing step=0 raises a ValueError:
range(0, 10, 0)
# ValueError: range() arg 3 must not be zero
Float arguments not supported
range() only accepts integers. For float sequences, use NumPy’s arange() or a custom implementation:
# This raises TypeError
range(0.5, 2.5)
# TypeError: 'float' object cannot be interpreted as an integer
See Also
- built-in::enumerate — iterate over a sequence with index and value
- built-in::len — get the length of a range (or any sequence)
- list::list — convert a range to a list for immediate access