In Python, range(stop) produces the integers 0, 1, ..., stop - 1, and range(start, stop, step) counts from start up to, but not including, stop. It is the standard way to repeat a for loop a number of times.
A range is lazy: it does not store its numbers, it computes them when asked, so range(10 ** 9) uses almost no memory. It is still a real sequence that supports len(), indexing, slicing, and in. Printing it shows its definition, such as range(0, 5), not the numbers.
range is a built-in type (a class) that you call like a function: each call creates a new range object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax
One, two, or three integer arguments.
python
for i in range(stop): ...
for i in range(start, stop): ...
for i in range(start, stop, step): ...
Call
What it does
range(5)
0, 1, 2, 3, 4.
range(2, 8)
2 up to 7.
range(10, 0, -3)
10, 7, 4, 1 (counting down).
list(range(3))
Returns [0, 1, 2].
Use enumerate() instead of range(len(items)) when you need both the index and the item.
Library declarations
python
range(stop, /)
range(start, stop, step=1, /)
# class range: an immutable built-in sequence type
Parameters
Parameter
Description
start
Optional. First number. Default 0.
stop
Required. The range stops before this number.
step
Optional. Difference between numbers; negative counts down. Default 1; must not be 0.
Return Value
Return Value
Description
range
An immutable, lazy sequence of ints.
Python Examples
Example 1 — range() with One, Two, and Three Arguments
List the numbers of several ranges and print a range object directly.