Hướng dẫn check not null python - kiểm tra không null python

Kiểm tra xem một biến không phải là null trong python #

Sử dụng toán tử is not để kiểm tra xem một biến không phải là null trong Python, ví dụ: ________số 8. Toán tử is not trả 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
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ớ).

Copied!

my_var = None # ✅ check if variable is NOT null if my_var is not None: print('variable does NOT store null') # ✅ check if variable is null if my_var is None: print('variable stores null')

Lưu ý rằng không có giá trị

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
1 trong Python.

Đối tượng

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
2 đại diện cho sự vắng mặt của một giá trị. Đây là tương đương với giá trị

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
1 trong các ngôn ngữ khác.

Câu lệnh

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 đầu tiên kiểm tra xem biến không lưu trữ giá trị

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
2 và bảng thứ hai - nếu biến lưu trữ giá trị

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
2.

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ị

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
2.

Khi chúng tôi sử dụng

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
9 hoặc 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ư

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
2 luôn luôn được thực hiện với

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
9 hoặc 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!

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 và không bằ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
5) 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ụ:

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
6.

Dưới đây là một ví dụ minh họa tốt hơn việc kiểm tra danh tính (

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
9 và is not) so với kiểm tra 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
4 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
5).

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

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
1 trong bộ nhớ.

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

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
5, Python gọi phương thức

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
3 trên đối tượng.

Đó là

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
4 gọi

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
5. Về lý thuyết, phương pháp này có thể được thực hiện theo một cách không thể đoán trước, vì vậy việc kiểm tra

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
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
9 và 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

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
9 trả về địa chỉ của đối tượng trong bộ nhớ trong cpython.

Nếu hai biến đề cập đến cùng một đối tượng, hàm

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
9 sẽ tạo ra cùng một kết quả.

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ị

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
2 cho hàm

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
9 luôn luôn trả về kết quả tương tự vì chỉ có một trường hợp

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
2 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 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!

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ì có nhiều giá trị giả khác không phải là

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
2.

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:

    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
    2 và

    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.
  • 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 (không) thuộc bất kỳ loại số nào
  • Trình tự trống và bộ sưu tập:

    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 (Chuỗi trống),

    Copied!

    print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
    0 (Tuple trống),

    Copied!

    print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
    1 (danh sách trống),

    Copied!

    print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
    2 (Từ điển trống),

    Copied!

    print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
    3 (bộ trống),

    Copied!

    print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
    4 (phạm vi trống).

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ỉ

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
2).