Từ khóa Python
Định nghĩa và cách sử dụng
Từ khóa None
được sử dụng để xác định giá trị null hoặc không có giá trị nào cả.
Không có gì không giống với 0, sai hoặc một chuỗi trống. Không có loại dữ liệu nào của riêng nó [không phải là] và không có ai có thể là không có.
Nhiều ví dụ hơn
Thí dụ
Nếu bạn làm một boolean nếu kiểm tra, điều gì sẽ xảy ra? Không đúng hay sai:
x = không có
Nếu x: & nbsp; in ["Bạn có nghĩ rằng không ai là đúng không?"] Elif x là sai: & nbsp; in ["Bạn có nghĩ không có gì là sai không?"] Khác: & nbsp; In ["Không có gì là không đúng, hoặc sai, không có gì chỉ là không ..."]
print["Do you think None is True?"]
elif x is False:
print ["Do you think None is False?"]
else:
print["None is not True, or False, None is just None..."]
Hãy tự mình thử »
Từ khóa Python
Kiểm tra xem một biến không có trong Python #
Sử dụng toán tử Copied!is
để kiểm tra xem một biến không có trong Python, ví dụ: if my_var is None:
. Toán tử is
trả về
1 nếu các giá trị ở hai bên bên trái và bên phải trỏ đến cùng một đối tượng và nên được sử dụng khi kiểm tra các singleton 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
None
.
Copied!
my_var = None # ✅ Check if variable is None if my_var is None: # 👇️ this runs print['variable is None'] # ✅ Check if variable is NOT None if my_var is not None: print['variable is not None']
Câu lệnh Copied!
3 đầu tiên kiểm tra xem biến có 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
None
không và câu thứ hai - nếu biến không lưu trữ giá trị None
.
Bạn nên sử dụng toán tử is
khi bạn cần kiểm tra xem biến có lưu trữ giá trị None
không.
Khi chúng tôi sử dụng is
, 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ư None
phải luôn luôn được thực hiện với is
hoặc
1 và không bao giờ là nhà khai thác bình đẳng.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
Sử dụng các toán tử bình đẳng [bằng Copied! Copied! Copied!
2 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
3] khi bạn cần kiểm tra xem giá trị có bằng với 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
4.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 [is
] so với kiểm tra bình đẳng [
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
7 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ử bằng kép, Python gọi phương thức
8 trên đối tượng.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à
9 gọiCopied!
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. 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
None
với toán tử is
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
3 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
3 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ị None
cho hàm
3 luôn luôn trả về kết quả tương tự vì chỉ có một trường hợp củaCopied!
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
None
trong chương trình Python.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.
Copied!
my_var = None # 👇️ checks if variable stores a truthy value if my_var: print['variable is truthy'] # 👇️ checks if variable stores a falsy value if not my_var: # 👇️ this runs print['variable is falsy']
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 lưu trữ giá trị None
vì có nhiều giá trị giả khác không phải là None
.
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:
None
và
1.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
2 [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ự và bộ sưu tập trống:
3 [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
4 [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
5 [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
6 [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
7 [bộ 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 [phạm vi 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
Nếu bạn kiểm tra xem một biến có phải là sự thật hay không, bạn đang kiểm tra xem biến không phải là bất kỳ giá trị giả nào đã nói ở trên, không chỉ None
.