Thủ thuật gán nhiều biến là một lối tắt cho phép bạn gán nhiều biến với các giá trị trong danh sách trong một dòng mã. Vì vậy, thay vì làm điều này
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
2Bạn có thể gõ dòng mã này
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
3Thủ thuật gán nhiều biến cũng có thể được sử dụng để hoán đổi các giá trị trong hai biến
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
4Phương pháp chỉ số
Phương thức
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
7 cho phép bạn tìm chỉ mục của một giá trị bằng cách chuyển tên của nó>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
5Thêm giá trị
nối thêm []
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
8 thêm một phần tử vào cuối một >>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
9>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
6chèn[]
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
# ['table', 'chair', 'rack', 'shelf']
>>> furniture[1:3]
# ['chair', 'rack']
>>> furniture[0:-1]
# ['table', 'chair', 'rack']
>>> furniture[:2]
# ['table', 'chair']
>>> furniture[1:]
# ['chair', 'rack', 'shelf']
>>> furniture[:]
# ['table', 'chair', 'rack', 'shelf']
0 thêm một phần tử vào một >>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
9 tại một vị trí nhất định>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
7Xóa giá trị
xóa []
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
# ['table', 'chair', 'rack', 'shelf']
>>> furniture[1:3]
# ['chair', 'rack']
>>> furniture[0:-1]
# ['table', 'chair', 'rack']
>>> furniture[:2]
# ['table', 'chair']
>>> furniture[1:]
# ['chair', 'rack', 'shelf']
>>> furniture[:]
# ['table', 'chair', 'rack', 'shelf']
2 xóa một mục bằng cách sử dụng chỉ mục>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
8gỡ bỏ[]
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
# ['table', 'chair', 'rack', 'shelf']
>>> furniture[1:3]
# ['chair', 'rack']
>>> furniture[0:-1]
# ['table', 'chair', 'rack']
>>> furniture[:2]
# ['table', 'chair']
>>> furniture[1:]
# ['chair', 'rack', 'shelf']
>>> furniture[:]
# ['table', 'chair', 'rack', 'shelf']
3 xóa một mục bằng cách sử dụng giá trị thực của nó>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
9Xóa các mục lặp đi lặp lại
Nếu giá trị xuất hiện nhiều lần trong danh sách, chỉ phiên bản đầu tiên của giá trị sẽ bị xóa
nhạc pop[]
Theo mặc định,
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
# ['table', 'chair', 'rack', 'shelf']
>>> furniture[1:3]
# ['chair', 'rack']
>>> furniture[0:-1]
# ['table', 'chair', 'rack']
>>> furniture[:2]
# ['table', 'chair']
>>> furniture[1:]
# ['chair', 'rack', 'shelf']
>>> furniture[:]
# ['table', 'chair', 'rack', 'shelf']
4 sẽ xóa và trả lại mục cuối cùng của danh sách. Bạn cũng có thể chuyển chỉ mục của phần tử dưới dạng tham số tùy chọn>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
0Sắp xếp các giá trị với sort[]
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
1Bạn cũng có thể chuyển ________ 85 cho đối số từ khóa ________ 86 để có ________ 87 sắp xếp các giá trị theo thứ tự ngược lại
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
2Nếu bạn cần sắp xếp các giá trị theo thứ tự bảng chữ cái thông thường, hãy chuyển
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
# ['table', 'chair', 'rack', 'shelf']
>>> furniture[1:3]
# ['chair', 'rack']
>>> furniture[0:-1]
# ['table', 'chair', 'rack']
>>> furniture[:2]
# ['table', 'chair']
>>> furniture[1:]
# ['chair', 'rack', 'shelf']
>>> furniture[:]
# ['table', 'chair', 'rack', 'shelf']
8 cho đối số từ khóa key trong lệnh gọi phương thức sort[]>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
3Bạn có thể sử dụng chức năng tích hợp sẵn
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0:4]
# ['table', 'chair', 'rack', 'shelf']
>>> furniture[1:3]
# ['chair', 'rack']
>>> furniture[0:-1]
# ['table', 'chair', 'rack']
>>> furniture[:2]
# ['table', 'chair']
>>> furniture[1:]
# ['chair', 'rack', 'shelf']
>>> furniture[:]
# ['table', 'chair', 'rack', 'shelf']
9 để trả về một danh sách mới>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[-1]
# 'shelf'
>>> furniture[-3]
# 'chair'
>>> f'The {furniture[-1]} is bigger than the {furniture[-3]}'
# 'The shelf is bigger than the chair'
4Kiểu dữ liệu Tuple
Tuples vs Danh sách
Sự khác biệt chính giữa bộ dữ liệu và danh sách là, trong khi
>>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
60 là các đối tượng không thể thay đổi, thì >>> furniture = ['table', 'chair', 'rack', 'shelf']
>>> furniture[0]
# 'table'
>>> furniture[1]
# 'chair'
>>> furniture[2]
# 'rack'
>>> furniture[3]
# 'shelf'
61 có thể thay đổi. Điều này có nghĩa là bộ dữ liệu không thể thay đổi trong khi danh sách có thể được sửa đổi. Bộ dữ liệu hiệu quả hơn bộ nhớ so với danh sách