40 Algorithms Every Programmer Should Know
上QQ阅读APP看书,第一时间看更新

The range function 

The range function can be used to easily generate a large list of numbers. It is used to auto-populate sequences of numbers in a list.

The range function is simple to use. We can use it by just specifying the number of elements we want in the list. By default, it starts from zero and increments by one:

>>> x = range(6)
>>> x
[0,1,2,3,4,5]

We can also specify the end number and the step:

>>> oddNum = range(3,29,2)
>>> oddNum
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27]

The preceding range function will give us odd numbers starting from 3 to 29.