range functionThe range function returns an immutable sequence object of integers between the given start integer to the stop integer.. rangestart, stop,[step] --> step size is optional
Trang 1range() function
The range() function returns an immutable sequence object of integers between the given start integer to the stop integer range(start, stop,[step]) > step size is optional
range(stop) -> We can give just stop size In this time, it will start from zero
as default
In [1]: range(0,15)
In [2]: print(*range(0,15))
In [3]: a = list(range(0,15))
print(a)
In [4]: a
In [5]: print(*range(7,15))
In [6]: start = 0
stop = 15
print(*range(start,stop))
If we don't specify starting value, it will start from 0 as default
In [7]: print(*range(10))
In [8]: print(*range(0,50,2)) #step size 2
Out[1]: range(0, 15)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
7 8 9 10 11 12 13 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
Trang 2In [9]: print(*range(1,99,3)) #step size 3
In [10]: print(*range(0,40,5)) #step size 5
In [11]: print(*range(20,0)) #countdown
In [12]: print(*range(15,0,-1)) # countdown 15 to 0
In [13]: for i in range(0,20):
print(i)
In [14]: for i in range(1,10,):
print(i*"+")
In [15]: for i in range(10,0,-1):
print(i * "+")
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79
82 85 88 91 94 97
0 5 10 15 20 25 30 35
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+
Trang 3In [16]: for i in range(10,0,-1):
print(i*"★")
This is the end of range function lesson
See you in our next lessons
In [ ]:
★★★★★★★★★★
★★★★★★★★★
★★★★★★★★
★★★★★★★
★★★★★★
★★★★★
★★★★
★★★
★★
★