[Nhà tài trợ] Bắt đầu học Python với hướng dẫn Giới thiệu về Python miễn phí của DataCamp. Tìm hiểu Khoa học dữ liệu bằng cách hoàn thành các thử thách mã hóa tương tác và xem video của các chuyên gia hướng dẫn. Bắt đầu bây giờ
Cập nhật ngày 07 tháng 1 năm 2020
Hàm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 được dùng để tạo một dãy số theo thời gian. Đơn giản nhất, nó chấp nhận một số nguyên và trả về một đối tượng phạm vi [một loại có thể lặp lại]. Trong Python 2,
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 trả về một
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>8 không hiệu quả lắm để xử lý dữ liệu lớn
Cú pháp của hàm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 như sau
cú pháp
range[[start,] stop [, step]] -> range object
THÔNG SỐ MÔ TẢ_______50[tùy chọn] Điểm bắt đầu của chuỗi. Nó mặc định là
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
1. print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
2 [bắt buộc]Điểm cuối của chuỗi. Mục này sẽ không được bao gồm trong trình tự. print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
3 [tùy chọn] Kích thước bước của trình tự. Nó mặc định là print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
4Bây giờ chúng ta hãy xem xét một vài ví dụ để hiểu cách hoạt động của
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6
ví dụ 1
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>
dùng thử
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
Khi
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 được gọi với một đối số duy nhất, nó sẽ tạo ra một chuỗi số từ
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
1 cho đến đối số được chỉ định [nhưng không bao gồm đối số đó]. Đó là lý do tại sao số print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
8 không có trong dãy sốví dụ 2
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6
dùng thử
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>7
Ở đây,
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 được gọi với hai đối số,
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
8 và >>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>61. Kết quả là nó sẽ tạo ra một dãy số từ
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
8 đến >>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>61 [nhưng không bao gồm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>61]
Bạn cũng có thể chỉ định số âm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>4
dùng thử
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>5
ví dụ 3
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6
dùng thử
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>7
Ở đây, hàm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 được gọi với đối số
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
3 là >>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>67, do đó, hàm này sẽ trả về mọi phần tử thứ ba từ
print[range[5]]
# list[] call is not required in Python 2
print[list[range[5]]]
4 đến >>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>69 [tất nhiên là không bao gồm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>69]
Bạn cũng có thể sử dụng đối số bước để đếm ngược
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>4
dùng thử
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>0
Hàm
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 thường được sử dụng với vòng lặp for để lặp lại một hành động với số lần nhất định. Ví dụ: trong danh sách sau, chúng tôi sử dụng
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 để thực thi thân vòng lặp 5 lần
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>1
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>2
dùng thử
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>3
Mã này có chức năng tương đương như sau
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>1
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>5
Tuy nhiên, trong mã thực tế, bạn nên luôn sử dụng
>>> >>> range[5] range[0, 5] >>> >>> list[range[5]] # list[] call is not required in Python 2 [0, 1, 2, 3, 4] >>>6 vì mã này ngắn gọn, linh hoạt và hoạt động tốt hơn
Hướng dẫn khác [Nhà tài trợ]
Trang web này được hỗ trợ rộng rãi bởi DataCamp. DataCamp cung cấp Hướng dẫn Python tương tác trực tuyến cho Khoa học dữ liệu. Tham gia cùng hơn một triệu người học khác và bắt đầu học Python cho khoa học dữ liệu ngay hôm nay