Kiểm tra xem một biến không có trong Python #
Sử dụng toán tử Copied!is not
để kiểm tra xem một biến không phải là không có trong Python, ví dụ: ________số 8. Toán tử is not
trả về
0 nếu các giá trị ở bên trái và bên phải không trỏ đến cùng một đối tượng [cùng một vị trí trong bộ nhớ].my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list # 👈️ same list as above
print[my_first_list is my_second_list] # 👉️ True
print[my_first_list == my_second_list] # 👉️ True
Copied!
my_var = None # ✅ check if variable is NOT none if my_var is not None: print['variable does NOT store None'] # ✅ check if variable is None if my_var is None: print['variable stores None']
Câu lệnh Copied! Copied! Copied!
1 đầu tiên kiểm tra xem biến không lưu trữ giá trị my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list # 👈️ same list as above
print[my_first_list is my_second_list] # 👉️ True
print[my_first_list == my_second_list] # 👉️ True
2 và bảng thứ hai - nếu biến lưu trữ giá trị my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list # 👈️ same list as above
print[my_first_list is my_second_list] # 👉️ True
print[my_first_list == my_second_list] # 👉️ True
2.my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list # 👈️ same list as above
print[my_first_list is my_second_list] # 👉️ True
print[my_first_list == my_second_list] # 👉️ True
Bạn nên sử dụng toán tử is not
khi bạn cần kiểm tra xem một biến không lưu trữ giá trị
2.Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
Khi chúng tôi sử dụng
6 hoặcCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
is not
, chúng tôi kiểm tra danh tính của đối tượng.Hướng dẫn theo phong cách PEP 8 đề cập rằng so sánh với các đơn lẻ như
2 luôn luôn được thực hiện vớiCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
6 hoặcCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
is not
, và không bao giờ là người vận hành bình đẳng.Sử dụng các toán tử bình đẳng [bằng Copied! Copied! Copied!
1 và không bằng my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list.copy[] # 👈️ copy created
print[my_first_list is my_second_list] # 👉️ False
print[my_first_list == my_second_list] # 👉️ True
2] khi bạn cần kiểm tra xem giá trị có bằng hoặc không bằng giá trị khác không, ví dụ: my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list.copy[] # 👈️ copy created
print[my_first_list is my_second_list] # 👉️ False
print[my_first_list == my_second_list] # 👉️ True
3.my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list.copy[] # 👈️ copy created
print[my_first_list is my_second_list] # 👉️ False
print[my_first_list == my_second_list] # 👉️ True
Dưới đây là một ví dụ minh họa tốt hơn việc kiểm tra danh tính [
6 vàCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
is not
] so với kiểm tra bình đẳng [1 vàCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy[] # 👈️ copy created print[my_first_list is my_second_list] # 👉️ False print[my_first_list == my_second_list] # 👉️ True
2].Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy[] # 👈️ copy created print[my_first_list is my_second_list] # 👉️ False print[my_first_list == my_second_list] # 👉️ True
Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
Chúng tôi đã tuyên bố 2 biến lưu trữ cùng một danh sách.
Chúng tôi đặt biến thứ hai thành biến thứ nhất, vì vậy cả hai biến đều chỉ vào cùng một đối tượng
8 trong bộ nhớ.Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy[] # 👈️ copy created print[my_first_list is my_second_list] # 👉️ False print[my_first_list == my_second_list] # 👉️ True
Bây giờ, hãy tạo một bản sao nông của danh sách và gán nó cho biến thứ hai.
Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy[] # 👈️ copy created print[my_first_list is my_second_list] # 👉️ False print[my_first_list == my_second_list] # 👉️ True
Lưu ý rằng kiểm tra danh tính không thành công. Mặc dù hai danh sách lưu trữ cùng một giá trị, theo cùng một thứ tự, chúng chỉ ra các vị trí khác nhau trong bộ nhớ [chúng không phải là cùng một đối tượng].
Khi chúng tôi sử dụng toán tử không bằng
2, Python gọi phương thứcCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy[] # 👈️ copy created print[my_first_list is my_second_list] # 👉️ False print[my_first_list == my_second_list] # 👉️ True
0 trên đối tượng.Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
Đó là
1 gọiCopied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
2. Về lý thuyết, phương pháp này có thể được thực hiện theo cách không thể đoán trước, vì vậy việc kiểm traCopied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
2 với các toán tửCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
6 vàCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
is not
trực tiếp hơn.Bạn có thể sử dụng hàm id [] để có được danh tính của một đối tượng.
Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
Hàm trả về một số nguyên, được đảm bảo là duy nhất và không đổi cho tuổi thọ của đối tượng.
Hàm
6 trả về địa chỉ của đối tượng trong bộ nhớ trong CPython.Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
Nếu hai biến đề cập đến cùng một đối tượng, hàm
6 sẽ tạo ra cùng một kết quả.Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
Chuyển giá trị
2 cho hàmCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
6 luôn luôn trả về kết quả tương tự vì chỉ có một trường hợpCopied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 139944523741504 my_second_list = my_first_list.copy[] print[id[my_second_list]] # 👉️ 139944522293184 print[id[my_first_list] == id[my_second_list]] # 👉️ False
2 trong chương trình Python.Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
Copied!
print[id[None]] # 👉️ 9817984 print[id[None]] # 👉️ 9817984
Bạn cũng có thể thấy các ví dụ trực tuyến kiểm tra sự thật và giả.
Copied!
my_var = None # 👇️ checks if variable stores a falsy value if not my_var: # 👇️ this runs print['variable is falsy'] # 👇️ checks if variable stores a truthy value if my_var: print['variable is truthy']
Tuy nhiên, điều này rất khác so với kiểm tra rõ ràng nếu một biến không lưu trữ giá trị Copied! Copied!
2 vì có nhiều giá trị giả khác không phải là my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list # 👈️ same list as above
print[my_first_list is my_second_list] # 👉️ True
print[my_first_list == my_second_list] # 👉️ True
2.my_first_list = ['a', 'b', 'c']
my_second_list = my_first_list # 👈️ same list as above
print[my_first_list is my_second_list] # 👉️ True
print[my_first_list == my_second_list] # 👉️ True
Tất cả các giá trị không phải là sự thật được coi là giả mạo. Các giá trị giả trong Python là:
- Các hằng số được xác định là giả mạo:
2 vàCopied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True
4.Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
5 [không] thuộc bất kỳ loại số nàoCopied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
- Trình tự trống và bộ sưu tập:
6 [Chuỗi trống],Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
7 [Tuple trống],Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
8 [danh sách trống],Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
9 [Từ điển trống],Copied!
my_first_list = ['a', 'b', 'c'] print[id[my_first_list]] # 👉️ 140311440685376 my_second_list = my_first_list print[id[my_second_list]] # 👉️ 140311440685376 print[id[my_first_list] == id[my_second_list]] # 👉️ True
0 [bộ trống],Copied!
print[id[None]] # 👉️ 9817984 print[id[None]] # 👉️ 9817984
1 [phạm vi trống].Copied!
print[id[None]] # 👉️ 9817984 print[id[None]] # 👉️ 9817984
Nếu bạn kiểm tra xem một biến có giả không, bạn đang kiểm tra xem biến có phải là bất kỳ giá trị giả nào đã nói ở trên không [không chỉ
2].Copied!
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print[my_first_list is my_second_list] # 👉️ True print[my_first_list == my_second_list] # 👉️ True