9.4) Range
The function range() can come in handy when looping over the indexes of a list.
It creates a list containing a progression of integers. Here’s how you use it:
<list>= range(<start>, <end>, <step>)
The default start value is 0, and the default step is 1, but the end value does not have a default so it must be defined as an argument. The list will include the start value, but not the end value. start, end and step should be integers.
For example:
>>> range(5) # uses the default start and step values, sets the end value to 5 [0, 1, 2, 3, 4] >>> range(2,5) # uses the default step value [2,3,4] >>> range(0,10,2) # manually sets the start, end and step values [0,2,4,6,8])
- Create a list of all the integers starting at -5 and going up to (and including) 5
- Create the same list, but in the reverse order, from 5 to -5