Chapters
Python114 chapters

ReferenceBuilt-in functions

range()

Whole numbers from start up to but not including stop, produced on demand.

Signature

range(start, stop, step)

Parameters

NameMeans
startthe first number, 0 when omitted
stopone past the last
stephow much to add each time

Returns

A range object, not a list. It costs nothing until you loop over it.

Example

Python
print(list(range(4)))
print(list(range(2, 6)))
print(list(range(0, 10, 3)))
print(list(range(3, 0, -1)))

Output

[0, 1, 2, 3]
[2, 3, 4, 5]
[0, 3, 6, 9]
[3, 2, 1]

See also