Next / Previous / Contents / TCC Help System / NM Tech homepage

20.33. range(): Generate an arithmetic progression as a list

This function generates a list containing the values of an arithmetic progression, that is, a sequence of numbers such that the difference between adjacent numbers is always the same. There are three forms:

range(n)

Returns the list [0, 1, 2, ..., n-1]. Note that the result never includes the value n.

range(start, stop)

Returns the list [start, start+1, start+2, ..., stop-1]. The result never includes the stop value.

range(start, stop, step)

Returns the list [start, start+step, start+2*step, ...], up to but not including the value of stop. The value of step may be negative.

Examples:

>>> range(4)
[0, 1, 2, 3]
>>> range(4,9)
[4, 5, 6, 7, 8]
>>> range(10,104,10)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> range(5,-1,-1)
[5, 4, 3, 2, 1, 0]