Tải bản đầy đủ (.pdf) (3 trang)

037 range function tủ tài liệu training

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (462.91 KB, 3 trang )

range() 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)
Out[1]: range(0, 15)
In [2]: print(*range(0,15))
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
In [3]: a = list(range(0,15))
print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
In [4]: a
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
In [5]: print(*range(7,15))
7 8 9 10 11 12 13 14
In [6]: start = 0
stop = 15
print(*range(start,stop))
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

If we don't specify starting value, it will start from 0 as default.
In [7]: print(*range(10))
0 1 2 3 4 5 6 7 8 9
In [8]: print(*range(0,50,2)) #step size 2
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

The Complete Python 3 Programming Course: (Beginner to Advanced)



In [9]: print(*range(1,99,3)) #step size 3
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
In [10]: print(*range(0,40,5)) #step size 5
0 5 10 15 20 25 30 35
In [11]: print(*range(20,0)) #countdown

In [12]: print(*range(15,0,-1)) # countdown 15 to 0
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
In [13]: for i in range(0,20):
print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

19
In [14]: for i in range(1,10,):
print(i*"+")
+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
In [15]: for i in range(10,0,-1):
print(i * "+")
++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+

The Complete Python 3 Programming Course: (Beginner to Advanced)


In [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 [ ]:

The Complete Python 3 Programming Course: (Beginner to Advanced)



×