Home / Python / Reference / Built-in functions

Python range() function

A sequence of whole numbers, usually for loops.

Syntax

range(stop) / range(start, stop, step=1)

Counts from start (default 0) up to but not including stop, in steps of step. A range uses almost no memory however long it is; wrap it in list() to see the numbers.

Parameters

startfirst number (default 0)
stopstop before this number
stepgap between numbers; negative counts down

Return value

range

Example

Code

print(list(range(5)))
print(list(range(2, 8)))
print(list(range(0, 20, 5)))
print(list(range(5, 0, -1)))
for i in range(3):
    print("lap", i)

Output

[0, 1, 2, 3, 4]
[2, 3, 4, 5, 6, 7]
[0, 5, 10, 15]
[5, 4, 3, 2, 1]
lap 0
lap 1
lap 2
Good to know. range(1, 10) stops at 9. Use range(1, 11) for 1 to 10.

Related

Learn it step by step in the free lesson Loops, with exercises that check your answers.