Hướng dẫn python bool to byte - bool python thành byte



Các khóa học qua video:
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript

Mục lục bài viết:

  • Kiểu Boolean Python
    • Python Booleans làm Từ khóa
    • Python Booleans dưới dạng số
  • Toán tử Boolean
    • Các nhà khai thác không có đầu vào
    • Toán tử không phải Boolean
    • Toán tử và Boolean
    • Toán tử hoặc Boolean
    • Các toán tử Boolean khác
  • Toán tử so sánh
    • Bình đẳng và Bất bình đẳng
    • So sánh đơn hàng
    • Nhà điều hành là
    • Nhà điều hành trong
    • Các toán tử so sánh chuỗi
  • Thử nghiệm Boolean trong Python
    • Không có giá trị Boolean
    • Các số dưới dạng giá trị Boolean
    • Chuỗi dưới dạng giá trị Boolean
    • Các loại khác dưới dạng giá trị Boolean
    • Ví dụ: Mảng NumPy
    • Toán tử và Chức năng
  • Phần kết luận

Show

Kiểu Boolean trong Python là một trong những kiểu dữ liệu tích hợp sẵn của Python . Nó được sử dụng để biểu thị giá trị chân lý của một biểu thức. Ví dụ, biểu thức 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
7là 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8, trong khi biểu thức 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
9là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. Việc hiểu cách các giá trị Boolean trong Python hoạt động rất quan trọng để lập trình tốt bằng Python.

Trong hướng dẫn này, bạn sẽ học cách:

  • Thao tác các giá trị Boolean với các toán tử Boolean
  • Chuyển đổi Boolean sang các loại khác
  • Chuyển đổi các loại khác sang Python Booleans
  • Sử dụng Python Booleans để viết mã Python hiệu quả và dễ đọc

Kiểu Boolean Python

Kiểu Boolean trong Python chỉ có hai giá trị có thể có:

  1. >>> lines="""\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """.splitlines()
    >>> sum("the" in line.lower() for line in lines) / len(lines)
    0.5
    
    8
  2. >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len(line_list)
    4
    >>> 2/4
    0.5
    
    0

Không có giá trị nào khác sẽ có 

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3như loại của nó. Bạn có thể kiểm tra loại 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8và 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0với cài đặt sẵn 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
6:

>>>

>>> type(False)

>>> type(True)

Của 

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
6cả hai 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0và 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3.

Kiểu 

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3được tích hợp sẵn , có nghĩa là nó luôn có sẵn bằng Python và không cần nhập. Tuy nhiên, bản thân tên không phải là một từ khóa trong ngôn ngữ. Mặc dù kiểu sau được coi là kiểu xấu, nhưng có thể gán cho tên 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3:

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'

>>> type(False)

>>> type(True)

Python Booleans làm Từ khóa

Python Booleans dưới dạng số

Toán tử Boolean

Các nhà khai thác không có đầu vào

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True

>>> type(False)

>>> type(True)

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False

>>> type(False)

>>> type(True)

Của >>> lines = """\ ... He took his vorpal sword in hand; ... Long time the manxome foe he sought— ... So rested he by the Tumtum tree ... And stood awhile in thought. ... """ >>> line_list = lines.splitlines() >>> "the" in line_list[0] False >>> "the" in line_list[1] True >>> 0 + False + True # Equivalent to 0 + 0 + 1 1 >>> ["the" in line for line in line_list] [False, True, True, False] >>> False + True + True + False 2 >>> len(line_list) 4 >>> 2/4 0.5 6cả hai >>> lines = """\ ... He took his vorpal sword in hand; ... Long time the manxome foe he sought— ... So rested he by the Tumtum tree ... And stood awhile in thought. ... """ >>> line_list = lines.splitlines() >>> "the" in line_list[0] False >>> "the" in line_list[1] True >>> 0 + False + True # Equivalent to 0 + 0 + 1 1 >>> ["the" in line for line in line_list] [False, True, True, False] >>> False + True + True + False 2 >>> len(line_list) 4 >>> 2/4 0.5 0và >>> lines="""\ ... He took his vorpal sword in hand; ... Long time the manxome foe he sought— ... So rested he by the Tumtum tree ... And stood awhile in thought. ... """.splitlines() >>> sum("the" in line.lower() for line in lines) / len(lines) 0.5 8là >>> lines = """\ ... He took his vorpal sword in hand; ... Long time the manxome foe he sought— ... So rested he by the Tumtum tree ... And stood awhile in thought. ... """ >>> line_list = lines.splitlines() >>> "the" in line_list[0] False >>> "the" in line_list[1] True >>> 0 + False + True # Equivalent to 0 + 0 + 1 1 >>> ["the" in line for line in line_list] [False, True, True, False] >>> False + True + True + False 2 >>> len(line_list) 4 >>> 2/4 0.5 3.

Kiểu 

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3được tích hợp sẵn , có nghĩa là nó luôn có sẵn bằng Python và không cần nhập. Tuy nhiên, bản thân tên không phải là một từ khóa trong ngôn ngữ. Mặc dù kiểu sau được coi là kiểu xấu, nhưng có thể gán cho tên 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0

>>> type(False)

>>> type(True)

Của 

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
6cả hai 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0và 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3.

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5

>>> type(False)

>>> type(True)

Để xem tại sao điều này hoạt động, bạn có thể chia đoạn mã trên thành các phần nhỏ hơn:

>>>

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5

Các 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
04biến giữ một danh sách các dòng. Dòng đầu tiên không có tiếng nói 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
6trong nó, vì vậy 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
06là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. Trong dòng thứ hai, 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
6không xuất hiện, vì vậy 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
09là 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. Vì Boolean là số, bạn có thể thêm chúng vào số và 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
11cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2.

Vì 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
13là danh sách bốn Boolean, bạn có thể thêm chúng lại với nhau. Khi bạn thêm 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
14, bạn nhận được 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
15. Bây giờ, nếu bạn chia kết quả đó 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
16cho độ dài của danh sách, bạn sẽ có được 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
17. Từ 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
6xuất hiện trong một nửa số dòng trong vùng chọn. Đây là một cách hữu ích để tận dụng thực tế rằng Boolean là các số.

Toán tử Boolean

Toán tử Boolean là những toán tử nhận đầu vào Boolean và trả về kết quả Boolean .

Lưu ý: Sau đó, bạn sẽ thấy rằng các toán tử này có thể được cung cấp các đầu vào khác và không phải lúc nào cũng trả về kết quả Boolean. Hiện tại, tất cả các ví dụ sẽ sử dụng đầu vào và kết quả Boolean. Bạn sẽ thấy cách điều này tổng quát cho các giá trị khác trong phần về độ tin cậy .

Vì các giá trị Boolean trong Python chỉ có hai tùy chọn khả thi, 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, có thể chỉ định các toán tử hoàn toàn theo kết quả mà chúng gán cho mọi kết hợp đầu vào có thể có. Các thông số kỹ thuật này được gọi là bảng sự thật vì chúng được hiển thị trong một bảng.

Như bạn sẽ thấy ở phần sau, trong một số tình huống, biết một đầu vào cho một toán tử là đủ để xác định giá trị của nó. Trong những trường hợp đó, đầu vào khác không được đánh giá. Đây được gọi là đánh giá ngắn mạch .

Tầm quan trọng của việc đánh giá ngắn mạch phụ thuộc vào từng trường hợp cụ thể. Trong một số trường hợp, nó có thể ít ảnh hưởng đến chương trình của bạn. Trong các trường hợp khác, chẳng hạn như khi cần tính toán nhiều để đánh giá các biểu thức không ảnh hưởng đến kết quả, nó mang lại lợi ích hiệu suất đáng kể. Trong những trường hợp khắc nghiệt nhất, tính đúng đắn của mã của bạn có thể phụ thuộc vào việc đánh giá ngắn mạch.

Các nhà khai thác không có đầu vào

Bạn có thể nghĩ về 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8và 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0như các toán tử Boolean mà không cần đầu vào. Một trong những toán tử này luôn trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8và toán tử kia luôn trả về 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Việc coi các giá trị Boolean trong Python như các toán tử đôi khi rất hữu ích. Ví dụ: cách tiếp cận này giúp nhắc nhở bạn rằng chúng không phải là các biến. Vì lý do tương tự mà bạn không thể gán cho 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
25, không thể gán cho 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Chỉ có hai giá trị Boolean trong Python tồn tại. Toán tử Boolean không có đầu vào luôn trả về cùng một giá trị. Bởi vì điều này, 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8và 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0là hai toán tử Boolean duy nhất không nhận đầu vào.

Các >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 30Boolean điều hành

Toán tử Boolean duy nhất có một đối số là 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30. Nó nhận một đối số và trả về kết quả ngược lại: 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0for 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8và 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8for 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. Đây là bảng sự thật:

Bảng này minh họa 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30trả về giá trị chân lý ngược lại của đối số. Vì 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30chỉ lấy một đối số nên nó không ngắn mạch. Nó đánh giá đối số của nó trước khi trả về kết quả:

>>>

>>> not True
False
>>> not False
True
>>> def print_and_true():
...     print("I got called")
...     return True
...
>>> not print_and_true()
I got called
False

Dòng cuối cùng cho thấy 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30đánh giá đầu vào của nó trước khi trả về 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Bạn có thể tự hỏi tại sao không có toán tử Boolean nào khác nhận một đối số duy nhất. Để hiểu lý do tại sao, bạn có thể nhìn vào bảng hiển thị tất cả các toán tử Boolean về mặt lý thuyết có thể sử dụng một đối số:

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
40
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
41
Danh tínhĐúngKhông
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0

Chỉ có bốn toán tử khả thi với một đối số. Ngoài ra 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30, ba toán tử còn lại đều có những cái tên hơi kỳ quái vì chúng không thực sự tồn tại:

  • >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    53: Vì toán tử này chỉ trả lại đầu vào của nó, bạn chỉ có thể xóa nó khỏi mã của mình mà không ảnh hưởng gì.

  • >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    54: Đây là một toán tử ngắn mạch vì nó không phụ thuộc vào đối số của nó. Bạn chỉ có thể thay thế nó bằng 
    >>> lines="""\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """.splitlines()
    >>> sum("the" in line.lower() for line in lines) / len(lines)
    0.5
    
    8và nhận được kết quả tương tự.

  • >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    56: Đây là một toán tử ngắn mạch khác vì nó không phụ thuộc vào đối số của nó. Bạn chỉ có thể thay thế nó bằng 
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len(line_list)
    4
    >>> 2/4
    0.5
    
    0và nhận được kết quả tương tự.

Không có toán tử nào khác có thể có với một đối số sẽ hữu ích.

Các >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 58Boolean điều hành

Các 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58nhà điều hành phải mất hai đối số. Nó đánh giá 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0trừ khi cả hai đầu vào 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. Bạn có thể xác định hành vi của 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58bằng bảng sự thật sau:

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
40
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
64
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
65
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0

Bảng này dài dòng. Tuy nhiên, nó minh họa hành vi tương tự như mô tả ở trên. Nếu 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
40là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, thì giá trị của 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
64không quan trọng. Bởi vì điều này, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58ngắn mạch nếu đầu vào đầu tiên là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. Nói cách khác, nếu đầu vào đầu tiên là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, thì đầu vào thứ hai không được đánh giá.

Mã sau có đầu vào thứ hai có tác dụng phụ , in ra, để cung cấp một ví dụ cụ thể:

>>>

>>> def print_and_return(x):
...     print(f"I am returning {x}")
...     return x
...
>>> True and print_and_return(True)
I am returning True
True
>>> True and print_and_return(False)
I am returning False
False
>>> False and print_and_return(True)
False
>>> False and print_and_return(False)
False

Trong hai trường hợp cuối cùng, không có gì được in. Hàm không được gọi vì việc gọi nó không cần thiết để xác định giá trị của 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58toán tử. Nhận thức được hiện tượng đoản mạch là rất quan trọng khi các biểu thức có tác dụng phụ. Trong hai ví dụ cuối cùng, việc đánh giá ngắn mạch ngăn cản tác dụng phụ khi in xảy ra.

Một ví dụ trong đó hành vi này có thể quan trọng là trong mã có thể tạo ra các ngoại lệ:

>>>

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False

Trong hai trường hợp cuối cùng, không có gì được in. Hàm không được gọi vì việc gọi nó không cần thiết để xác định giá trị của 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58toán tử. Nhận thức được hiện tượng đoản mạch là rất quan trọng khi các biểu thức có tác dụng phụ. Trong hai ví dụ cuối cùng, việc đánh giá ngắn mạch ngăn cản tác dụng phụ khi in xảy ra.

Một ví dụ trong đó hành vi này có thể quan trọng là trong mã có thể tạo ra các ngoại lệ:

Hàm >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 85này được thừa nhận là ngớ ngẩn và nhiều linters sẽ cảnh báo về việc biểu thức này >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 86là vô dụng. Nó phục vụ mục đích không thành công khi được cung cấp >>> def inverse_and_true(n): ... 1 // n ... return True ... >>> inverse_and_true(5) True >>> inverse_and_true(0) Traceback (most recent call last): File "", line 1, in File "", line 2, in inverse_and_true ZeroDivisionError: integer division or modulo by zero >>> False and inverse_and_true(0) False 4dưới dạng tham số vì phép chia cho >>> def inverse_and_true(n): ... 1 // n ... return True ... >>> inverse_and_true(5) True >>> inverse_and_true(0) Traceback (most recent call last): File "", line 1, in File "", line 2, in inverse_and_true ZeroDivisionError: integer division or modulo by zero >>> False and inverse_and_true(0) False 4không hợp lệ. Tuy nhiên, dòng cuối cùng không nêu ra một ngoại lệ. Do đánh giá ngắn mạch, hàm không được gọi, phép chia >>> def inverse_and_true(n): ... 1 // n ... return True ... >>> inverse_and_true(5) True >>> inverse_and_true(0) Traceback (most recent call last): File "", line 1, in File "", line 2, in inverse_and_true ZeroDivisionError: integer division or modulo by zero >>> False and inverse_and_true(0) False 4cho không xảy ra và không có ngoại lệ nào được nêu ra.

Ngược lại, 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
90sẽ nêu ra một ngoại lệ. Trong trường hợp đó, giá trị của đầu vào thứ hai sẽ cần thiết cho kết quả của 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58. Khi đầu vào thứ hai được đánh giá, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92sẽ được gọi, nó sẽ chia cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4và một ngoại lệ sẽ được đưa ra.

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
40
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
64
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
01
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0

Bảng này dài dòng. Tuy nhiên, nó minh họa hành vi tương tự như mô tả ở trên. Nếu 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
40là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, thì giá trị của 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
64không quan trọng. Bởi vì điều này, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58ngắn mạch nếu đầu vào đầu tiên là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. Nói cách khác, nếu đầu vào đầu tiên là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, thì đầu vào thứ hai không được đánh giá.

Mã sau có đầu vào thứ hai có tác dụng phụ , in ra, để cung cấp một ví dụ cụ thể:

  • >>>

  • Trong hai trường hợp cuối cùng, không có gì được in. Hàm không được gọi vì việc gọi nó không cần thiết để xác định giá trị của 

    >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    58toán tử. Nhận thức được hiện tượng đoản mạch là rất quan trọng khi các biểu thức có tác dụng phụ. Trong hai ví dụ cuối cùng, việc đánh giá ngắn mạch ngăn cản tác dụng phụ khi in xảy ra.

Một ví dụ trong đó hành vi này có thể quan trọng là trong mã có thể tạo ra các ngoại lệ:

Hàm 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
85này được thừa nhận là ngớ ngẩn và nhiều linters sẽ cảnh báo về việc biểu thức này 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
86là vô dụng. Nó phục vụ mục đích không thành công khi được cung cấp 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4dưới dạng tham số vì phép chia cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4không hợp lệ. Tuy nhiên, dòng cuối cùng không nêu ra một ngoại lệ. Do đánh giá ngắn mạch, hàm không được gọi, phép chia 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4cho không xảy ra và không có ngoại lệ nào được nêu ra.

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
0

Trong hai trường hợp cuối cùng, không có gì được in. Hàm không được gọi vì việc gọi nó không cần thiết để xác định giá trị của 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58toán tử. Nhận thức được hiện tượng đoản mạch là rất quan trọng khi các biểu thức có tác dụng phụ. Trong hai ví dụ cuối cùng, việc đánh giá ngắn mạch ngăn cản tác dụng phụ khi in xảy ra.

Một ví dụ trong đó hành vi này có thể quan trọng là trong mã có thể tạo ra các ngoại lệ:

Hàm 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
85này được thừa nhận là ngớ ngẩn và nhiều linters sẽ cảnh báo về việc biểu thức này 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
86là vô dụng. Nó phục vụ mục đích không thành công khi được cung cấp 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4dưới dạng tham số vì phép chia cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4không hợp lệ. Tuy nhiên, dòng cuối cùng không nêu ra một ngoại lệ. Do đánh giá ngắn mạch, hàm không được gọi, phép chia 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4cho không xảy ra và không có ngoại lệ nào được nêu ra.

Ngược lại, 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
90sẽ nêu ra một ngoại lệ. Trong trường hợp đó, giá trị của đầu vào thứ hai sẽ cần thiết cho kết quả của 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58. Khi đầu vào thứ hai được đánh giá, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92sẽ được gọi, nó sẽ chia cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4và một ngoại lệ sẽ được đưa ra.

Có mười sáu toán tử Boolean hai đầu vào có thể có. Ngoại trừ 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58và 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
94, chúng hiếm khi cần thiết trong thực tế. Bởi vì điều này, 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8, 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58, và 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
94được chỉ được xây dựng trong các nhà khai thác Python Boolean.

Toán tử so sánh

Một số toán tử của Python kiểm tra xem liệu mối quan hệ có giữ giữa hai đối tượng hay không. Vì mối quan hệ giữ hoặc không giữ, các toán tử này, được gọi là toán tử so sánh , luôn trả về các giá trị Boolean.

Các toán tử so sánh là nguồn phổ biến nhất của các giá trị Boolean.

Bình đẳng và Bất bình đẳng

Các toán tử so sánh phổ biến nhất là toán tử bình đẳng ( 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46) và toán tử bất bình đẳng ( 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47) . Hầu như không thể viết bất kỳ lượng mã Python có ý nghĩa nào mà không sử dụng ít nhất một trong những toán tử đó.

Toán tử bình đẳng ( 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46) là một trong những toán tử được sử dụng nhiều nhất trong mã Python. Bạn thường cần so sánh một kết quả chưa biết với một kết quả đã biết hoặc hai kết quả chưa biết với nhau. Một số hàm trả về các giá trị cần được so sánh với một trạm gác để xem liệu một số điều kiện cạnh đã được phát hiện hay chưa. Đôi khi bạn cần so sánh kết quả từ hai hàm với nhau.

Toán tử bình đẳng thường được sử dụng để so sánh các số:

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
1

Bạn có thể đã sử dụng toán tử bình đẳng trước đây. Chúng là một số toán tử phổ biến nhất trong Python. Đối với tất cả các đối tượng Python dựng sẵn và đối với hầu hết các lớp của bên thứ ba, chúng trả về giá trị Boolean : 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Lưu ý: Ngôn ngữ Python không thực thi điều đó 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47trả về Booleans. Các thư viện như NumPy và pandas trả về các giá trị khác.

Chỉ đứng sau toán tử bình đẳng phổ biến là toán tử bất bình đẳng ( 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47). Nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8nếu các đối số không bằng nhau và 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0nếu chúng có. Các ví dụ tương tự nhau trên phạm vi rộng. Nhiều bài kiểm tra đơn vị kiểm tra rằng giá trị không bằng một giá trị không hợp lệ cụ thể. Ứng dụng khách web có thể kiểm tra xem có phải mã lỗi không 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56trước khi thử một giải pháp thay thế.

Dưới đây là hai ví dụ về toán tử bất đẳng thức Python đang được sử dụng:

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
2

Bạn có thể đã sử dụng toán tử bình đẳng trước đây. Chúng là một số toán tử phổ biến nhất trong Python. Đối với tất cả các đối tượng Python dựng sẵn và đối với hầu hết các lớp của bên thứ ba, chúng trả về giá trị Boolean : 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Lưu ý: Ngôn ngữ Python không thực thi điều đó 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47trả về Booleans. Các thư viện như NumPy và pandas trả về các giá trị khác.

Chỉ đứng sau toán tử bình đẳng phổ biến là toán tử bất bình đẳng ( 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47). Nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8nếu các đối số không bằng nhau và 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0nếu chúng có. Các ví dụ tương tự nhau trên phạm vi rộng. Nhiều bài kiểm tra đơn vị kiểm tra rằng giá trị không bằng một giá trị không hợp lệ cụ thể. Ứng dụng khách web có thể kiểm tra xem có phải mã lỗi không 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56trước khi thử một giải pháp thay thế.

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
3

Bạn có thể đã sử dụng toán tử bình đẳng trước đây. Chúng là một số toán tử phổ biến nhất trong Python. Đối với tất cả các đối tượng Python dựng sẵn và đối với hầu hết các lớp của bên thứ ba, chúng trả về giá trị Boolean : 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Lưu ý: Ngôn ngữ Python không thực thi điều đó >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 46và >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 47trả về Booleans. Các thư viện như NumPy và pandas trả về các giá trị khác.

Chỉ đứng sau toán tử bình đẳng phổ biến là toán tử bất bình đẳng ( 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47). Nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8nếu các đối số không bằng nhau và 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0nếu chúng có. Các ví dụ tương tự nhau trên phạm vi rộng. Nhiều bài kiểm tra đơn vị kiểm tra rằng giá trị không bằng một giá trị không hợp lệ cụ thể. Ứng dụng khách web có thể kiểm tra xem có phải mã lỗi không 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56trước khi thử một giải pháp thay thế.

  • Dưới đây là hai ví dụ về toán tử bất đẳng thức Python đang được sử dụng:
  • Có lẽ điều đáng ngạc nhiên nhất về toán tử bất đẳng thức Python là thực tế là nó tồn tại ngay từ đầu. Rốt cuộc, bạn có thể đạt được kết quả tương tự như 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    57với 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    58. Python thường tránh các cú pháp bổ sung, và đặc biệt là các toán tử lõi phụ, vì những thứ có thể dễ dàng đạt được bằng các phương tiện khác.

Tuy nhiên, bất bình đẳng được sử dụng thường xuyên đến mức cần có một nhà điều hành riêng cho nó. Trong các phiên bản cũ của Python, trong 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
59loạt bài này, thực sự có hai cú pháp khác nhau.

 Như một trò đùa của ngày Cá tháng Tư, Python vẫn hỗ trợ một cú pháp thay thế cho sự bất bình đẳng với việc 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
60nhập đúng :
Điều này không bao giờ được sử dụng trong bất kỳ mã nào dành cho mục đích sử dụng thực tế. Tuy nhiên, nó có thể hữu ích cho đêm đố Python tiếp theo của bạn.
So sánh đơn hàngMột tập hợp các toán tử kiểm tra khác là các toán tử so sánh thứ tự . Có bốn toán tử so sánh đơn hàng có thể được phân loại theo hai chất lượng:Hướng : Nó nhỏ hơn hay lớn hơn?
Tính chặt chẽ : Có được phép bình đẳng hay không?Vì hai lựa chọn là độc lập, bạn nhận được 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
61các toán tử so sánh đơn hàng. Tất cả bốn được liệt kê trong bảng này:
Ít hơn
Lớn hơnLớn hơnLớn hơn

Nghiêm khắc

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
62

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
4

Bạn có thể đã sử dụng toán tử bình đẳng trước đây. Chúng là một số toán tử phổ biến nhất trong Python. Đối với tất cả các đối tượng Python dựng sẵn và đối với hầu hết các lớp của bên thứ ba, chúng trả về giá trị Boolean : 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Lưu ý: Ngôn ngữ Python không thực thi điều đó 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47trả về Booleans. Các thư viện như NumPy và pandas trả về các giá trị khác.

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
5

Bạn có thể đã sử dụng toán tử bình đẳng trước đây. Chúng là một số toán tử phổ biến nhất trong Python. Đối với tất cả các đối tượng Python dựng sẵn và đối với hầu hết các lớp của bên thứ ba, chúng trả về giá trị Boolean : 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8hoặc 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Khi các toán tử so sánh thứ tự được xác định, nói chung chúng trả về một Boolean.

Lưu ý : Python không bắt buộc các toán tử so sánh trả về Booleans. Trong khi tất cả các đối tượng Python được tích hợp sẵn và hầu hết các đối tượng của bên thứ ba, trả về Boolean khi được so sánh, vẫn có những ngoại lệ.

Ví dụ, các toán tử so sánh giữa các mảng NumPy hoặc gấu trúc DataFrames trả về mảng và DataFrames. Bạn sẽ thấy thêm về sự tương tác của các giá trị NumPy và Boolean ở phần sau trong hướng dẫn này.

So sánh các số trong Python là một cách phổ biến để kiểm tra các điều kiện biên. Lưu ý rằng điều 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
62đó không cho phép bình đẳng, trong khi 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
64đó:

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
6

Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng chúng trả về một giá trị Python Boolean.

Người >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 69điều hành

Người 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69điều hành kiểm tra danh tính đối tượng . Nói cách khác, chỉ 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
71đánh giá 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8khi 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
73và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
74đánh giá cho cùng một đối tượng. Các 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69nhà khai thác có ngược lại, các 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76nhà điều hành.

Một cách sử dụng điển hình của 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76là để so sánh danh sách cho danh tính:

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
7

Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng chúng trả về một giá trị Python Boolean.

Người 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69điều hành

The >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 86 Operator

Người 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69điều hành kiểm tra danh tính đối tượng . Nói cách khác, chỉ 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
71đánh giá 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8khi 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
73và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
74đánh giá cho cùng một đối tượng. Các 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69nhà khai thác có ngược lại, các 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76nhà điều hành.

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
8

Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng chúng trả về một giá trị Python Boolean.

Người 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69điều hành

>>>

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
9

Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng chúng trả về một giá trị Python Boolean.

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
0

Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng chúng trả về một giá trị Python Boolean.

Người 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69điều hành

Người >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 69điều hành kiểm tra danh tính đối tượng . Nói cách khác, chỉ >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 71đánh giá >>> lines="""\ ... He took his vorpal sword in hand; ... Long time the manxome foe he sought— ... So rested he by the Tumtum tree ... And stood awhile in thought. ... """.splitlines() >>> sum("the" in line.lower() for line in lines) / len(lines) 0.5 8khi >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 73và >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 74đánh giá cho cùng một đối tượng. Các >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 69nhà khai thác có ngược lại, các >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 76nhà điều hành.

Một cách sử dụng điển hình của 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76là để so sánh danh sách cho danh tính:

Even though 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
79, they are not the same object. The 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76 operator always returns the opposite of 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69. There’s no difference between the expression 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
82 and the expression 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
83 except for readability.

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
1

Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng chúng trả về một giá trị Python Boolean.

Người 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69điều hành

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
2

Trong trường hợp này, các phần của chuỗi đánh giá theo các Boolean sau:

  • >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    34 Là 
    >>> lines="""\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """.splitlines()
    >>> sum("the" in line.lower() for line in lines) / len(lines)
    0.5
    
    8
  • >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    36 Là 
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len(line_list)
    4
    >>> 2/4
    0.5
    
    0

Điều này có nghĩa là một trong các kết quả là 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8và một là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. Vì 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
40bằng 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0, giá trị của toàn bộ chuỗi là 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0.

Bạn có thể kết hợp các loại và hoạt động trong một chuỗi so sánh miễn là các loại có thể được so sánh:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
3

Các toán tử không nhất thiết phải giống nhau. Thậm chí không phải tất cả các loại đều giống nhau. Trong các ví dụ trên, bạn có ba kiểu số:

  1. >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    43
  2. >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    44
  3. >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len(line_list)
    4
    >>> 2/4
    0.5
    
    3

Đây là ba kiểu số khác nhau, nhưng bạn có thể so sánh các đối tượng thuộc các kiểu số khác nhau mà không gặp vấn đề gì.

Đánh giá chuỗi ngắn mạch

Nếu dây chuyền sử dụng ngầm định 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58, thì dây chuyền cũng phải ngắn mạch. Điều này rất quan trọng vì ngay cả trong trường hợp so sánh đơn hàng không được xác định, thì một chuỗi vẫn có thể trả về 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
4

Mặc dù Python không thể so sánh thứ tự số nguyên và số chuỗi, nhưng sẽ 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
48đánh giá 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0vì nó không đánh giá so sánh thứ hai. Trong trường hợp này, đánh giá ngắn mạch ngăn chặn một tác dụng phụ khác: nêu ra một ngoại lệ.

Đánh giá ngắn mạch của chuỗi so sánh có thể ngăn ngừa các trường hợp ngoại lệ khác:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
5

Chia 

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2theo 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4lẽ ra sẽ nâng cao a 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
52. Tuy nhiên, vì đánh giá ngắn mạch, Python không đánh giá sự phân chia không hợp lệ. Điều này có nghĩa là Python bỏ qua việc đánh giá không chỉ so sánh mà còn cả các đầu vào cho phép so sánh.

Một khía cạnh khác đó là quan trọng để hiểu về chuỗi so sánh là khi Python thực hiện đánh giá một yếu tố trong chuỗi, nó đánh giá nó một lần duy nhất:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
6

Bởi vì các yếu tố trung được đánh giá chỉ một lần, nó không phải lúc nào cũng an toàn để cấu trúc lại 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
53để 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
54. Mặc dù chuỗi hoạt động giống như 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58trong đánh giá ngắn mạch, nhưng nó đánh giá tất cả các giá trị, bao gồm cả các giá trị trung gian, chỉ một lần.

Chains are especially useful for range checks, which confirm that a value falls within a given range. For example, in a daily invoice that includes the number hours worked, you might do the following:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
7

If there are 

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4 hours worked, then there’s no reason to send the invoice. Accounting for Daylight Saving Time, the maximum number of hours in a day is 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
57. The above range check confirms that the number of hours worked in a day falls within the allowable range.

Mixing Operators and Chaining

Until now, all our examples involved 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46, 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47, and the order comparisons. However, you can chain all of Python’s comparison operators. This can lead to surprising behavior:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
8

Because 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
60 is a comparison chain, it evaluates to 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. You can break the chain into its parts:

  • The expression 
    >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    62 is 
    >>> lines="""\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """.splitlines()
    >>> sum("the" in line.lower() for line in lines) / len(lines)
    0.5
    
    8, as it would be for any value evaluated against itself.
  • The expression 
    >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    64 is 
    >>> lines="""\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """.splitlines()
    >>> sum("the" in line.lower() for line in lines) / len(lines)
    0.5
    
    8 since 
    >>> def inverse_and_true(n):
    ...     1 // n
    ...     return True
    ...
    >>> inverse_and_true(5)
    True
    >>> inverse_and_true(0)
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in inverse_and_true
    ZeroDivisionError: integer division or modulo by zero
    >>> False and inverse_and_true(0)
    False
    
    4 is less than 
    >>> def inverse_and_true(n):
    ...     1 // n
    ...     return True
    ...
    >>> inverse_and_true(5)
    True
    >>> inverse_and_true(0)
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in inverse_and_true
    ZeroDivisionError: integer division or modulo by zero
    >>> False and inverse_and_true(0)
    False
    
    2.

Since both parts are 

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8, the chain evaluates to 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

However, people who are used to other operators in Python may assume that, like other expressions involving multiple operators such as 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
70, Python inserts parentheses into to the expression. However, neither way of inserting parenthesis will evaluate to 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

You can see why both evaluate to 

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0 if you break up the expressions. If you break up the first expression, you get the following:

>>>

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
9

You can see above that 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
62 returns 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8, as it would for any value. This means that 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
75 is the same as 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
76. Booleans are numeric types, and 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8 is equal to 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2. So 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
76 is the same as 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
80. Since this is a strict inequality, and 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
81, it returns False.

The second expression works differently:

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
0

Since 

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4 is less than 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2, 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
64 returns 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. Since 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
86, then it can’t be the case that 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
87.

Note: Don’t take the above 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
88 lightly. Using 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69 on numbers can be confusing. However, specifically for cases in which you know the numbers are not equal, you can know that 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69 will also return 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0. While this example is correct, it’s not an example of good Python coding style.

The most important lesson to draw from this is that chaining comparisons with 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69 usually isn’t a good idea. It confuses the reader and probably isn’t necessary.

Like 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69, the 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86 operator and its opposite, 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12, can often yield surprising results when chained:

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
1

Để giảm thiểu sự nhầm lẫn, ví dụ này xâu chuỗi các phép so sánh với các toán tử khác nhau và sử dụng 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86với các chuỗi để kiểm tra các chuỗi con. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt! Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu tại sao nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

Cuối cùng, bạn có thể xâu chuỗi 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76với 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12:

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
2

Để giảm thiểu sự nhầm lẫn, ví dụ này xâu chuỗi các phép so sánh với các toán tử khác nhau và sử dụng 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86với các chuỗi để kiểm tra các chuỗi con. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt! Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu tại sao nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

Cuối cùng, bạn có thể xâu chuỗi >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 76với >>> False = 5 File "", line 1 SyntaxError: cannot assign to False 12:

Lưu ý rằng thứ tự của 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30hai toán tử không giống nhau! Các toán tử phủ định là 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76và 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12. Điều này tương ứng với cách sử dụng thông thường trong tiếng Anh, nhưng rất dễ mắc lỗi khi sửa đổi mã.

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
3

Để giảm thiểu sự nhầm lẫn, ví dụ này xâu chuỗi các phép so sánh với các toán tử khác nhau và sử dụng 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86với các chuỗi để kiểm tra các chuỗi con. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt! Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu tại sao nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

Cuối cùng, bạn có thể xâu chuỗi 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76với 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12:

Lưu ý rằng thứ tự của >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 30hai toán tử không giống nhau! Các toán tử phủ định là >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 76và >>> False = 5 File "", line 1 SyntaxError: cannot assign to False 12. Điều này tương ứng với cách sử dụng thông thường trong tiếng Anh, nhưng rất dễ mắc lỗi khi sửa đổi mã.

Thử nghiệm Boolean trong Python

Cách sử dụng phổ biến nhất cho Python Boolean là trong một 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03câu lệnh . Câu lệnh này sẽ thực thi nếu giá trị là 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8:

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
05chỉ được gọi khi biểu thức đánh giá là 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. Tuy nhiên, trong Python, bạn có thể đưa ra bất kỳ giá trị nào 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03. Các giá trị mà 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03coi 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8được gọi là truthy , và các giá trị mà 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03coi 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0được gọi là falsy .

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
4

Để giảm thiểu sự nhầm lẫn, ví dụ này xâu chuỗi các phép so sánh với các toán tử khác nhau và sử dụng 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86với các chuỗi để kiểm tra các chuỗi con. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt! Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu tại sao nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

Cuối cùng, bạn có thể xâu chuỗi >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 76với >>> False = 5 File "", line 1 SyntaxError: cannot assign to False 12:

Lưu ý rằng thứ tự của 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30hai toán tử không giống nhau! Các toán tử phủ định là 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76và 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12. Điều này tương ứng với cách sử dụng thông thường trong tiếng Anh, nhưng rất dễ mắc lỗi khi sửa đổi mã.

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
5

Để giảm thiểu sự nhầm lẫn, ví dụ này xâu chuỗi các phép so sánh với các toán tử khác nhau và sử dụng 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86với các chuỗi để kiểm tra các chuỗi con. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt! Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu tại sao nó trả về 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8.

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
6

Cuối cùng, bạn có thể xâu chuỗi 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76với 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12:

Lưu ý rằng thứ tự của 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30hai toán tử không giống nhau! Các toán tử phủ định là 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
76và 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
12. Điều này tương ứng với cách sử dụng thông thường trong tiếng Anh, nhưng rất dễ mắc lỗi khi sửa đổi mã.

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
7

Thử nghiệm Boolean trong Python

Cách sử dụng phổ biến nhất cho Python Boolean là trong một 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03câu lệnh . Câu lệnh này sẽ thực thi nếu giá trị là 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8:

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
8

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
05chỉ được gọi khi biểu thức đánh giá là 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. Tuy nhiên, trong Python, bạn có thể đưa ra bất kỳ giá trị nào 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03. Các giá trị mà 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03coi 
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8được gọi là truthy , và các giá trị mà 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03coi 
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0được gọi là falsy .

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03quyết định giá trị nào là trung thực và giá trị nào là sai bằng cách gọi nội bộ 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
13. Bạn đã gặp phải 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
13kiểu Boolean trong Python. Khi được gọi, nó chuyển đổi các đối tượng thành Booleans.

>>>

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
9

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
15 dưới dạng Giá trị Boolean

Đối tượng singleton >>> True == 1 True >>> False == 0 True >>> True + (False / True) 1.0 15luôn là giả:

Điều này thường hữu ích trong các 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03câu lệnh kiểm tra một giá trị sentinel. Tuy nhiên, tốt hơn là bạn nên kiểm tra rõ ràng danh tính với 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
18. Đôi khi 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
15có thể hữu ích khi kết hợp với đánh giá ngắn mạch để có giá trị mặc định.

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
0

Ví dụ: bạn có thể sử dụng 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
94để thay thế 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
15bằng một danh sách trống:

Các loại khác dưới dạng giá trị Boolean

Trừ khi các loại có một 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
42hoặc xác định cụ thể xem chúng là trung thực hay giả dối, chúng luôn luôn trung thực. Điều này đúng với các kiểu cài sẵn cũng như do người dùng xác định. Đặc biệt, các hàm luôn trung thực:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
1

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
2

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

Điều này có thể xảy ra do dấu ngoặc đơn bị quên hoặc tài liệu gây hiểu lầm không đề cập đến việc bạn cần gọi hàm. Nếu bạn mong đợi một giá trị Boolean trong Python nhưng có một hàm trả về giá trị Boolean, thì nó sẽ luôn là trung thực.

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
3

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
4

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
5

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

Điều này có thể xảy ra do dấu ngoặc đơn bị quên hoặc tài liệu gây hiểu lầm không đề cập đến việc bạn cần gọi hàm. Nếu bạn mong đợi một giá trị Boolean trong Python nhưng có một hàm trả về giá trị Boolean, thì nó sẽ luôn là trung thực.

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
6

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
7

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
8

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:

Điều này có thể xảy ra do dấu ngoặc đơn bị quên hoặc tài liệu gây hiểu lầm không đề cập đến việc bạn cần gọi hàm. Nếu bạn mong đợi một giá trị Boolean trong Python nhưng có một hàm trả về giá trị Boolean, thì nó sẽ luôn là trung thực.

Theo mặc định, các loại do người dùng xác định luôn trung thực:

Tạo một lớp trống làm cho mọi đối tượng của lớp đó là trung thực. Tất cả các đối tượng là trung thực trừ khi các phương thức đặc biệt được xác định. Nếu bạn muốn tạo một số trường hợp giả mạo lớp của mình, bạn có thể xác định 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46:

>>>

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
9

Bạn cũng có thể sử dụng 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46để làm cho một đối tượng không trung thực cũng không giả dối:

Câu 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03lệnh cũng sử dụng 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46. Nó làm như vậy để đánh giá xem đối tượng là thật hay giả, từ đó xác định nhánh nào sẽ thực thi.

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
0

Nếu bạn định nghĩa 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
50phương thức trên một lớp, thì các thể hiện của nó có a 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
42. Trong trường hợp đó, giá trị Boolean của các thể hiện sẽ là sai chính xác khi độ dài của chúng là 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4:

Trong ví dụ này, 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
53sẽ trả về 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4trước và 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55sau đó. Tuy nhiên, điều ngược lại là không đúng. Việc xác định 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46không cung cấp độ dài cho các trường hợp:

Định nghĩa 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46không làm cho các cá thể của một trong hai lớp có a 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
42. Khi cả hai 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46và 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
60được xác định, 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46ưu tiên:

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
1

Mặc dù 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
73có chiều dài 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
63, nó vẫn sai.

Ví dụ: Mảng NumPy

Ví dụ trên có vẻ giống như điều gì đó chỉ xảy ra khi bạn viết một lớp nhằm chứng minh các trường hợp cạnh trong Python. Tuy nhiên, có thể nhận được kết quả tương tự bằng cách sử dụng một trong những thư viện phổ biến nhất trên PyPI : NumPy .

Mảng , như số, là sai hoặc trung thực tùy thuộc vào cách chúng so sánh với 

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4:

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
2

Mặc dù 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
73có độ dài là 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2, nhưng nó vẫn sai vì giá trị của nó là 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4.

Khi mảng có nhiều hơn một phần tử, một số phần tử có thể là sai và một số có thể là trung thực. Trong những trường hợp đó, NumPy sẽ đưa ra một ngoại lệ:

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
3

Ngoại lệ là quá dài dòng nên để dễ đọc, mã sử dụng xử lý văn bản để bao bọc các dòng.

Một trường hợp cạnh thú vị hơn liên quan đến các mảng trống. Bạn có thể tự hỏi liệu những thứ đó là giả dối như các trình tự khác hay là sự thật bởi vì chúng không bằng 

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4. Như bạn đã thấy ở trên, đó không phải là hai câu trả lời khả thi duy nhất. Các mảng cũng có thể từ chối có giá trị Boolean.

Thật thú vị, không có tùy chọn nào trong số này là hoàn toàn đúng:

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
4

Ví dụ này tận dụng sự sai lệch của 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
15và thực tế là 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
94không chỉ đoản mạch mà còn trả về giá trị cuối cùng được đánh giá. Mã để in báo cáo thêm 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
83vào đối số 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
76. Việc bổ sung 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
83này giúp bạn tránh được các lỗi chỉ với một thay đổi mã nhỏ.

Các chức năng được tích hợp sẵn 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
86và 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
87đánh giá độ trung thực cũng như đoản mạch, nhưng chúng không trả về giá trị cuối cùng được đánh giá. 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
86kiểm tra xem tất cả các đối số của nó có trung thực hay không:

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
5

Trong dòng cuối cùng, 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
86không đánh giá 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
90cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2. Kể từ khi 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
92là 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4, điều này sẽ huy động được 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
52.

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
87 kiểm tra xem bất kỳ đối số nào của nó là trung thực hay không:

>>>

>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
6

Trong dòng cuối cùng, 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
87không đánh giá 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
97cho 
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4.

Phần kết luận

Python Boolean là một kiểu dữ liệu được sử dụng phổ biến với nhiều ứng dụng hữu ích. Bạn có thể sử dụng Booleans với những hoạt động thích 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
58, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
94, 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
86, 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69, 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
46, và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
47để so sánh giá trị và kiểm tra các thành viên, danh tính, hoặc bình đẳng. Bạn cũng có thể sử dụng thử nghiệm Boolean với một 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03câu lệnh để kiểm soát luồng chương trình của bạn dựa trên tính trung thực của một biểu thức.

Trong hướng dẫn này, bạn đã học cách:

  • Thao tác các giá trị Boolean với các toán tử Boolean
  • Chuyển đổi Boolean sang các loại khác
  • Chuyển đổi các loại khác sang Python Booleans
  • Sử dụng Booleans để viết mã Python hiệu quả và dễ đọc

Bây giờ bạn biết cách đánh giá ngắn mạch hoạt động và nhận ra mối liên hệ giữa Booleans và 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
03câu lệnh. Kiến thức này sẽ giúp bạn hiểu được mã hiện có và tránh những cạm bẫy phổ biến có thể dẫn đến lỗi trong chương trình của riêng bạn.

Hướng dẫn python bool to byte - bool python thành byte

Các khóa học qua video:
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript
« Prev: Python: Các kiểu dữ liệu cơ bản trong Python Prev: Python: Các kiểu dữ liệu cơ bản trong Python
» Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp