Python range Function

Python range(...) function generates a list of arithmetic progressions. range(...) syntax: range(n): from 0 to n, step 1. 0, 1, 2, 3, .... n range(m,n): from m to n, step 1. m, m+1, m+2 ... n range(m,n,d): from m to n, step d. m, m+d, m+2d, ... n

>>> x = range(5)
>>> x
range(0,5)
>>> for i in x: print(i)
0
1
2
3
4
>>> x = range(2,7)
>>> for i in x: print(i)
2
3
4
5
6
>>> x = range(6,20,3)
>>> for i in x: print(i)
6
9
12
15
18
>>> x = range(40,20,-3)
>>> for i in x: print(i)
40
37
34
31
28
25
22
Using range function to loop throught array and list data:
>>> x = [3,67,3,2,10,43,2]
>>> for i in range(len(x)): print(x[i])
...
3
67
3
2
10
43
2










endmemo.com © 2024  | Terms of Use | Privacy | Home