Hướng dẫn python check if multiple variables are none - python kiểm tra xem có nhiều biến không

Tôi biết đây là một câu hỏi cũ, nhưng tôi muốn thêm một câu trả lời mà tôi tin là tốt hơn.

Nếu tất cả các yếu tố phải được kiểm tra có thể băm, bạn có thể sử dụng một bộ thay vì danh sách hoặc tuple.

>>> None not in {1, 84, 'String', (6, 'Tuple'), 3}

Điều này nhanh hơn nhiều so với các phương pháp trong các câu trả lời khác.

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop

Một lợi thế khác của phương pháp này là nó cung cấp cho bạn câu trả lời chính xác ngay cả khi ai đó định nghĩa phương thức __eq__ của một lớp để luôn trả về True. .

class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)

Kiểm tra xem nhiều biến không có trong Python #

Để kiểm tra xem nhiều biến không phải là không:

  1. Bọc các biến trong một danh sách.
  2. Sử dụng một biểu thức máy phát để lặp qua danh sách.
  3. Trên mỗi lần lặp, kiểm tra xem mục danh sách hiện tại không phải là
    $ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
    200000 loops, best of 5: 999 nsec per loop
    
    $ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
    2000000 loops, best of 5: 184 nsec per loop
    
    $ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
    2000000 loops, best of 5: 184 nsec per loop
    
    python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
    5000000 loops, best of 5: 48.6 nsec per loop
    
    2.
  4. Chuyển đối tượng Trình tạo đến hàm
    $ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
    200000 loops, best of 5: 999 nsec per loop
    
    $ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
    2000000 loops, best of 5: 184 nsec per loop
    
    $ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
    2000000 loops, best of 5: 184 nsec per loop
    
    python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
    5000000 loops, best of 5: 48.6 nsec per loop
    
    3.

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')

Chúng tôi đã sử dụng dấu ngoặc vuông để thêm các biến vào danh sách và sử dụng biểu thức trình tạo vào lặp qua danh sách.

Biểu thức của máy phát được sử dụng để thực hiện một số hoạt động cho mọi phần tử hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.

Trên mỗi lần lặp, chúng tôi kiểm tra xem mục danh sách hiện tại không phải là

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2 và trả về kết quả.

Bước cuối cùng là chuyển đối tượng

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
5 cho hàm
$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
3.

Chức năng tích hợp tất cả () lấy một sự khác biệt như một đối số và trả về True nếu tất cả các yếu tố của điều đó là sự thật (hoặc điều đó là trống rỗng).

Nếu tất cả các biến không phải là

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2, hàm
$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
3 sẽ trả về True, nếu không
class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
1 được trả về.

Một cách tiếp cận khác là sử dụng toán tử

class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
2.

Để kiểm tra xem nhiều biến không phải là không:

  1. Bọc các biến trong một chuỗi (ví dụ: một tuple hoặc một danh sách).
  2. Sử dụng toán tử
    class Int(int):
        def __eq__(self, other):
            return True
        def __hash__(self):
            return hash(super())
    
    print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
    print(None not in [1, Int(6), 2])                 # False (wrong)
    print(None not in (1, Int(6), 2))                 # False (wrong)
    print(None not in {1, Int(6), 2})                 # True (correct)
    
    3 để kiểm tra xem
    $ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
    200000 loops, best of 5: 999 nsec per loop
    
    $ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
    2000000 loops, best of 5: 184 nsec per loop
    
    $ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
    2000000 loops, best of 5: 184 nsec per loop
    
    python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
    5000000 loops, best of 5: 48.6 nsec per loop
    
    2 không phải là thành viên của chuỗi.
  3. Nếu chuỗi không chứa
    $ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
    200000 loops, best of 5: 999 nsec per loop
    
    $ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
    2000000 loops, best of 5: 184 nsec per loop
    
    $ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
    2000000 loops, best of 5: 184 nsec per loop
    
    python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
    5000000 loops, best of 5: 48.6 nsec per loop
    
    2, thì các biến không phải là
    $ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
    200000 loops, best of 5: 999 nsec per loop
    
    $ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
    2000000 loops, best of 5: 184 nsec per loop
    
    $ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
    2000000 loops, best of 5: 184 nsec per loop
    
    python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
    5000000 loops, best of 5: 48.6 nsec per loop
    
    2.

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')

Cách tiếp cận này trông đơn giản hơn nhiều so với phương pháp trước. Tuy nhiên, các toán tử

class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
2 và
class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
3 kiểm tra sự bình đẳng, ví dụ:
class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
9,

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
0, v.v.

Đây không phải là một thông lệ tốt trong Python, vì nên kiểm tra

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2 bằng cách sử dụng từ khóa

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
2.

Có một số trường hợp rất hiếm hoi khi sử dụng bằng và không bằng để kiểm tra

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2 có thể dẫn đến hành vi khó hiểu, do đó, Hướng dẫn kiểu PEP8 khuyên bạn nên sử dụng

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
2 và

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
5 khi kiểm tra
$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2.

Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
7 đánh giá thành True nếu

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
9 là thành viên của

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')
0, nếu không nó sẽ đánh giá thành
class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
1.

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')
2 trả về sự phủ định của

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
7.

Một cách tiếp cận khác là sử dụng toán tử

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')
4 nhiều lần.

Copied!

a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')

Điều này thường không được khuyến khích, vì nó khá lặp đi lặp lại và khó đọc.

Câu lệnh

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')
5 đầu tiên kiểm tra xem biến

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')
6 không phải là
$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2. Nếu nó, các đoạn ngắn mạch

Copied!

a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')
5 trả về
class Int(int):
    def __eq__(self, other):
        return True
    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2])                 # False (wrong)
print(None not in (1, Int(6), 2))                 # False (wrong)
print(None not in {1, Int(6), 2})                 # True (correct)
1 mà không kiểm tra bất kỳ điều kiện nào sau đây.

Biểu thức

Copied!

a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
0 trả về giá trị bên trái nếu nó giả mạo, nếu không giá trị bên phải được trả về.

Copied!

result = False and 'hello' print(result) # 👉️ False

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:
    $ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
    200000 loops, best of 5: 999 nsec per loop
    
    $ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
    2000000 loops, best of 5: 184 nsec per loop
    
    $ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
    2000000 loops, best of 5: 184 nsec per loop
    
    python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
    5000000 loops, best of 5: 48.6 nsec per loop
    
    2 và
    class Int(int):
        def __eq__(self, other):
            return True
        def __hash__(self):
            return hash(super())
    
    print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
    print(None not in [1, Int(6), 2])                 # False (wrong)
    print(None not in (1, Int(6), 2))                 # False (wrong)
    print(None not in {1, Int(6), 2})                 # True (correct)
    
    1.
  • Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    3 (không) thuộc bất kỳ loại số nào
  • Trình tự và bộ sưu tập trống:

    Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    4 (Chuỗi trống),

    Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    5 (Tuple trống),

    Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    6 (danh sách trống),

    Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    7 (Từ điển trống),

    Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    8 (bộ trống),

    Copied!

    a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')
    9 (phạm vi trống).

Vì vậy, nếu giá trị bên trái là bất kỳ giá trị giả nào đã nói ở trên, giá trị bên trái sẽ được trả về.

Tốt nhất là sử dụng hàm

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
3 để kiểm tra xem nhiều biến không phải là
$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2, vì phương pháp này không có bất kỳ trường hợp cạnh nào và khá dễ đọc.

Copied!

a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')

Làm thế nào để bạn kiểm tra xem nhiều biến không có trong Python?

Tốt nhất là sử dụng hàm tất cả () để kiểm tra xem nhiều biến không phải là không, vì phương pháp này không có bất kỳ trường hợp cạnh nào và khá dễ đọc.use the all() function to check if multiple variables are not None , as this approach doesn't have any edge cases and is quite readable.

Làm cách nào để kiểm tra nhiều biến trong Python?

Để kiểm tra nhiều biến x, y, z so với giá trị trong python, hãy sử dụng giá trị biểu thức trong {x, y, z}.Kiểm tra tư cách thành viên trong một bộ có độ phức tạp thời gian chạy liên tục.Do đó, đây là cách hiệu quả nhất để kiểm tra nhiều biến so với giá trị.use the expression value in {x, y, z} . Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value.

Làm thế nào để bạn biết nếu một biến là không?

Sử dụng toán tử IS để kiểm tra xem một biến không có trong Python, ví dụ:Nếu my_var không có:.Toán tử IS trả về true nếu các giá trị ở bên trái và bên phải chỉ vào cùng một đối tượng và nên được sử dụng khi kiểm tra các singletons như không có. to check if a variable is None in Python, e.g. if my_var is None: . The is operator returns True if the values on the left-hand and right-hand sides point to the same object and should be used when checking for singletons like None .

Làm thế nào để bạn kiểm tra nhiều biến cho bình đẳng so với một giá trị duy nhất?

Nếu bạn có trường hợp ngược lại và bạn có nhiều biến bạn cần kiểm tra so với một giá trị, bạn có thể trao đổi các cạnh trái và bên phải của toán tử trong.Vì vậy, thay vì sử dụng hoặc các toán tử như thế này: >>> a, b, c = 3.1415, 'xin chào', 42 >>> nếu a == 'xin chào' hoặc b == 'xin chào' hoặc c == 'xin chào':...swap the left and right sides of the in operator. So instead of using or operators like this: >>> a, b, c = 3.1415, 'hello', 42 >>> if a == 'hello' or b == 'hello' or c == 'hello': ...