Range
The built-in range() function is used to generate a sequence of numbers
within a range. You specify an optional start position (the default is 0)
and an end position. You can also specify an optional step (increment, decrement)
parameter.
range() only stores those three parameters (start, end, step), and thus is very
efficient in terms of memory footprint.
range() shows up a lot in for loops. It may be used to generate the values
used in the processing, but more often than not its purpose is to control
the number of iterations.
A range is not a list. If you want a list, you must consume it:
nums = range(2, 6)
list(nums) # → [2, 3, 4, 5]
list(range(0, 10, 2)) # → even numbers between 0 and 10
list(range(5, 0, -1)) # → countdown from 5
Ranges can be sliced just like lists:
nums = range(10)
nums[2:6] # → range(2, 6)
nums[::2] # → range(0, 10, 2)
Ranges are often used in for loops. See for loop