The term arithmetic progression refers to a sequence of numbers such that the difference between any two successive elements is the same. Examples: [1, 2, 3, 4, 5]; [10, 20, 30, 40]; [88, 77, 66, 55, 44, 33].
Python's built-in range() function returns
a list containing an arithmetic progression. There are
three different ways to call this function.
To generate the sequence [0, 1, 2, ..., n-1], use the form range(.
n)
>>> range(6) [0, 1, 2, 3, 4, 5] >>> range(2) [0, 1] >>> range(0) []
Note that the sequence will never include the value of
the argument ; it stops one value short.
n
To generate a sequence [i,
i+1, i+2, ..., n-1], use the form range(:
i, n)
>>> range(5,11) [5, 6, 7, 8, 9, 10] >>> range(1,5) [1, 2, 3, 4]
To generate an arithmetic progression with a difference
between
successive values, use the three-argument form drange(. The resulting sequence will be [i, n, d)i, i+d, i+2*d, ...],
and will stop before it reaches a value equal to .
n
>>> range ( 10, 100, 10 ) [10, 20, 30, 40, 50, 60, 70, 80, 90] >>> range ( 100, 0, -10 ) [100, 90, 80, 70, 60, 50, 40, 30, 20, 10] >>> range ( 8, -1, -1 ) [8, 7, 6, 5, 4, 3, 2, 1, 0]