Python Function Reference

range

Create a sequence of integers with the given step.

Python 3.13
range(stop)
range(start, stop[, step])

Parameters

startintoptional
The first number of the sequence, 0 by default.
stopint
The end of the sequence; this number is not included.
stepintoptional
The difference between numbers, 1 by default; can be negative but not zero.

Return value

A range object, an immutable sequence; wrap it in list() to get a list.

Examples

Numbers from 0 to 4

Python 3.13
print(list(range(5)))
[0, 1, 2, 3, 4]

Start, stop and step

Python 3.13
print(list(range(2, 10, 3)))
[2, 5, 8]

Countdown with a negative step

Python 3.13
print(list(range(5, 0, -1)))
[5, 4, 3, 2, 1]

Details

range(5) produces the numbers from 0 to 4. The numbers are not stored in memory but computed during iteration, so even range(10**9) is created instantly.

See also

Where to learn