Range
Python range() function¶
The range()
function gives you a bunch of numbers. An ordered bunch of numbers. Simple series like 1, 2, 3, 4
.
Now, the output type of the range()
function is a range
object. Don't get confused, the output of range() is always a bunch of numbers.
The range()
function and for
loops are like best buddies in Python. One creates a sequence of numbers, and the other loops through it.
range() syntax¶
range() examples¶
print(*(i for i in range(3)))
0
1
2
range(3) -> 0, 1, 2
range(10)[-1]
9
print(*(fruits[i] for i in range(len(fruits))))
apple
banana
cherry
date
* unpacks the items
print(*(i for i in range(10, 0, -2)))
10
8
6
4
2
Backwards using minus number
list( range( 5))
[0,1,2,3,4]
print(*(fruits[i] for i in range(len(fruits))))