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)