Hướng dẫn define true false python - xác định true false python



Các khóa học miễn phí qua video:
Lập trình C Java 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

Nội dung chính

Show
  • 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à
  • The in Operator
  • Nhà điều hành trong
  • 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


Nội dung chính

Python Booleans as Numbers

  • Các notBoolean điều hành
  • Các andBoolean điều hành
  • Các orBoolean điều hành
  • Other Boolean Operators

Kiểu Boolean Python

Người isđiều hành

  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

Chaining Comparison Operators

None dưới dạng Giá trị Boolean

>>> type(False)

>>> type(True)

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:

None dưới dạng Giá trị Boolean

>>> bool

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

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.

Python Booleans làm Từ khóa

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

None dưới dạng Giá trị Boolean

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

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.

None dưới dạng Giá trị Boolean

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

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.

Python Booleans dưới dạng số

Toán tử Boolean

None dưới dạng Giá trị Boolean

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

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.

For example, if you want to analyze a verse in a classic children’s poem to see what fraction of lines contain the word 

>>> 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
6, then the fact that 
>>> 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 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.
... """
>>> 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 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
4 can come in quite handy:

>>>

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

Tính tổng tất cả các giá trị trong một biểu thức trình tạo như thế này cho phép bạn biết số lầ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
8xuất hiện trong trình tạo. Số lầ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
8trong bộ tạo bằng số dòng có chứa 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
6, không phân biệt chữ hoa chữ thường. Chia số này cho tổng số dòng sẽ cho bạn tỷ lệ giữa các dòng phù hợp với tổng số dòng.

Để 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

Tính tổng tất cả các giá trị trong một biểu thức trình tạo như thế này cho phép bạn biết số lầ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
8xuất hiện trong trình tạo. Số lầ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
8trong bộ tạo bằng số dòng có chứa 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
6, không phân biệt chữ hoa chữ thường. Chia số này cho tổng số dòng sẽ cho bạn tỷ lệ giữa các dòng phù hợp với tổng số dòng.

Để 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:

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 notBoolean điều hành

>>>

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

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ả:

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
40
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ố: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
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'
    
    52: 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'
    
    53: Đâ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'
    
    55: Đâ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 andBoolean điều hành

Các 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57nhà đ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'
57bằng bảng sự thật sau:

>>> bool

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

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
63
>>> 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

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'
52: 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'
53: Đâ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ự.

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

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
55: Đâ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.

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
53: Đâ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ự.

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

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
55: Đâ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 andBoolean điều hành

Các 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57nhà đ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'
57bằng bảng sự thật sau:

>>> bool

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

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
98
>>> 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'
39là 
>>> 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'
62không quan trọng. Bởi vì điều này, 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57ngắ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'
    
    57toá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'
83nà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'
84là 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'
53: Đâ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'
0

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
55: Đâ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ự.

Other Boolean Operators

The mathematical theory of Boolean logic determines that no other operators beyond 

>>> 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'
57, and 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92 are needed. All other operators on two inputs can be specified in terms of these three operators. All operators on three or more inputs can be specified in terms of operators of two inputs.

Trong thực tế, thậm chí có cả hai 

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57là thừa. Các 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57nhà điều hành có thể được xác định theo 
>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92, và các 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92nhà điều hành có thể được xác định theo 
>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57. Tuy nhiên, 
>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92hữu ích đến mức tất cả các ngôn ngữ lập trình đều có cả hai.

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'
57và 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92, 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'
57, và 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92đượ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
43) 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
44) . 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
43) 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
43và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
44trả 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
44). 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
53trướ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
43và 
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
44trả 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
44). 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
53trướ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 43và >>> a_true_alias = True >>> a_true_alias True >>> True = 5 File "", line 1 SyntaxError: cannot assign to True 44trả 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
44). 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
53trướ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
    
    54với 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    55. 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
56loạ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
57nhậ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:
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
60
Không nghiêm khắc
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
61
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
62
     

Có hai lựa chọn về hướng đi và hai lựa chọn về sự nghiêm khắc. Điều này dẫn đến tổng cộng bốn toán tử so sánh đơn hàng.

Các toán tử so sánh thứ tự không được xác định cho tất cả các đối tượng. Một số đối tượng không có thứ tự có ý nghĩa. Mặc dù các danh sách và bộ giá trị được sắp xếp theo thứ tự từ điển , nhưng từ điển không có thứ tự có ý nghĩa:

>>>

>>> bool

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

Mặc dù chuỗi và số nguyên được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ trợ:

>>>

>>> bool

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

Mặc dù chuỗi và số nguyên được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ trợ:

Một lần nữa, vì không có cách rõ ràng để xác định thứ tự, Python từ chối so sánh chúng. Điều này tương tự với toán tử cộng ( 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
25). Mặc dù bạn có thể thêm chuỗi vào chuỗi và số nguyên vào số nguyên, việc thêm chuỗi vào số nguyên sẽ dẫn đến một ngoại lệ.

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

>>>

>>> bool

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

Mặc dù chuỗi và số nguyên được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ trợ:

Một lần nữa, vì không có cách rõ ràng để xác định thứ tự, Python từ chối so sánh chúng. Điều này tương tự với toán tử cộng ( 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
25). Mặc dù bạn có thể thêm chuỗi vào chuỗi và số nguyên vào số nguyên, việc thêm chuỗi vào số nguyên sẽ dẫn đến một ngoại lệ.

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.

>>>

>>> bool

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

Mặc dù chuỗi và số nguyên được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ trợ:

The in Operator

Một lần nữa, vì không có cách rõ ràng để xác định thứ tự, Python từ chối so sánh chúng. Điều này tương tự với toán tử cộng ( 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
25). Mặc dù bạn có thể thêm chuỗi vào chuỗi và số nguyên vào số nguyên, việc thêm chuỗi vào số nguyên sẽ dẫn đến một ngoại lệ.

>>>

>>> bool

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

Mặc dù chuỗi và số nguyên được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ trợ:

>>>

>>> bool

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

>>>

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

Không rõ ràng từ điển nên được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự mơ hồ, Python từ chối đoán.

Mặc dù chuỗi và số nguyên được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ trợ:

Một lần nữa, vì không có cách rõ ràng để xác định thứ tự, Python từ chối so sánh chúng. Điều này tương tự với toán tử cộng ( >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 25). Mặc dù bạn có thể thêm chuỗi vào chuỗi và số nguyên vào số nguyên, việc thêm chuỗi vào số nguyên sẽ dẫn đến một ngoại lệ.

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.

Biểu thức & nbsp; ________ 309 & nbsp; là một chuỗi toán tử so sánh. Nó có các biểu thức được phân tách bởi các toán tử so sánh. Kết quả là & nbsp; ________ 58 & nbsp; bởi vì cả hai phần của chuỗi đều là & nbsp; ________ 58. Bạn có thể phá vỡ chuỗi để xem nó hoạt động như thế nào:

>>>

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

Vì & nbsp; ________ 312 & nbsp; returns & nbsp; Một chuỗi so sánh tương đương với việc sử dụng & nbsp; ________ 157 & nbsp; trên tất cả các liên kết của nó. Trong trường hợp này, kể từ & nbsp; ________ 319 & nbsp; returns & nbsp; ________ 58, kết quả của toàn bộ chuỗi là & nbsp; ________ 58. Điều này có nghĩa là nếu bất kỳ liên kết nào là & nbsp; ____ 60, thì toàn bộ chuỗi là & nbsp; ________ 60:

Chuỗi so sánh này 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
0vì không phải tất cả các liên kết của nó đều như vậ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. Bởi vì các chuỗi so sánh là một 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57toán tử ngầm , nếu ngay cả một liên kế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, thì toàn bộ chuỗi là & nbsp; ________ 60. & nbsp;

>>>

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

Vì & nbsp; ________ 312 & nbsp; returns & nbsp; Một chuỗi so sánh tương đương với việc sử dụng & nbsp; ________ 157 & nbsp; trên tất cả các liên kết của nó. Trong trường hợp này, kể từ & nbsp; ________ 319 & nbsp; returns & nbsp; ________ 58, kết quả của toàn bộ chuỗi là & nbsp; ________ 58. Điều này có nghĩa là nếu bất kỳ liên kết nào là & nbsp; ____ 60, thì toàn bộ chuỗi là & nbsp; ________ 60:

  • Chuỗi so sánh này 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
    
    0vì không phải tất cả các liên kết của nó đều như vậ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. Bởi vì các chuỗi so sánh là một 
    >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    57toán tử ngầm , nếu ngay cả một liên kế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, thì toàn bộ chuỗi là & nbsp; ________ 60. & nbsp;
  • Trong trường hợp nào

________ 329 & nbsp; Là & nbsp; ________ 58

________ 331 & nbsp; Là & nbsp; ________ 60

>>>

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

Vì & nbsp; ________ 312 & nbsp; returns & nbsp; Một chuỗi so sánh tương đương với việc sử dụng & nbsp; ________ 157 & nbsp; trên tất cả các liên kết của nó. Trong trường hợp này, kể từ & nbsp; ________ 319 & nbsp; returns & nbsp; ________ 58, kết quả của toàn bộ chuỗi là & nbsp; ________ 58. Điều này có nghĩa là nếu bất kỳ liên kết nào là & nbsp; ____ 60, thì toàn bộ chuỗi là & nbsp; ________ 60:

  1. >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    38
  2. >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    39
  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

Chuỗi so sánh này 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
0vì không phải tất cả các liên kết của nó đều như vậ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. Bởi vì các chuỗi so sánh là một 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
57toán tử ngầm , nếu ngay cả một liên kế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, thì toàn bộ chuỗi là & nbsp; ________ 60. & nbsp;

Trong trường hợp nào

________ 329 & nbsp; Là & nbsp; ________ 58

>>>

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

________ 331 & nbsp; Là & nbsp; ________ 60

ĐiềU

>>>

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

Bạn Có đó là Kết

Các toán tử không nhất thiết ph

>>>

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

Đây là ba kiểu số khát nhau, nhưng bạn đó

Đuổi

>>>

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

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'
57, 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 :

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
43đá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ệ.

Đuổi

>>>

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

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
47. 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ỉ Vì vậy, Sánh Mà Cuy Cả Các Đầu Vào Cho Phépo So SánH.

  • Một khía cạNH, khát ệ
  • 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
    
    48để 
    >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    49. Mặc dù chuỗi hoạt động giống như 
    >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    57trong đánh giá ngắn mạch, nhưng nó đánh giá Tất Cả Các Giá trị, Bao

Các chuỗi đặc biệt hữu ích cho & nbsp; kiểm tra phạm vi, xác nhận rằng giá trị nằm trong một phạm vi nhất định. Ví dụ: trong hóa đơn hàng ngày bao gồm số giờ làm việc, bạn có thể làm như sau:

Nếu có & nbsp; ________ 94 & nbsp; giờ làm việc, thì không có lý do gì để gửi hóa đơn. Kế toán thời gian tiết kiệm ánh sáng ban ngày, số giờ tối đa trong một ngày là & nbsp; ________ 352. Kiểm tra phạm vi trên xác nhận rằng số giờ làm việc trong một ngày nằm trong phạm vi cho phép.

Bạn có thể thấy lý do tại sao cả hai đánh giá đến & nbsp; ________ 60 & nbsp; nếu bạn phá vỡ các biểu thức. Nếu bạn phá vỡ biểu thức đầu tiên, bạn sẽ nhận được như sau:

>>>

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

Bạn có thể thấy ở trên rằng & nbsp; ________ 357 & nbsp; returns & nbsp; ________ 58, như đối với bất kỳ giá trị nào. Điều này có nghĩa là & nbsp; ________ 370 & nbsp; giống như & nbsp; ________ 371. Booleans là các loại số và & nbsp; ________ 58 & nbsp; bằng & nbsp; ________ 92. Vì vậy, & nbsp; ________ 371 & nbsp; giống như & nbsp; ________ 375. Vì đây là A & nbsp; nghiêm ngặt & nbsp; bất bình đẳng và & nbsp; ________ 376, nó trả về sai.

Biểu thức thứ hai hoạt động khác nhau:

>>>

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

Bạn có thể thấy ở trên rằng & nbsp; ________ 357 & nbsp; returns & nbsp; ________ 58, như đối với bất kỳ giá trị nào. Điều này có nghĩa là & nbsp; ________ 370 & nbsp; giống như & nbsp; ________ 371. Booleans là các loại số và & nbsp; ________ 58 & nbsp; bằng & nbsp; ________ 92. Vì vậy, & nbsp; ________ 371 & nbsp; giống như & nbsp; ________ 375. Vì đây là A & nbsp; nghiêm ngặt & nbsp; bất bình đẳng và & nbsp; ________ 376, nó trả về sai.

Biểu thức thứ hai hoạt động khác nhau:

Vì & nbsp; ________ 94 & nbsp; ít hơn & nbsp; ________ 92, & nbsp; ________ 359 & nbsp; returns & nbsp; ________ 58. Vì & nbsp; ________ 381, thì nó có thể là trường hợp mà & nbsp; ________ 382.

Lưu ý: Don Tiết lấy các phần trên & nbsp; ________ 383 & nbsp; nhẹ. Sử dụng & nbsp; ________ 266 & nbsp; trên các số có thể là & nbsp; khó hiểu. Tuy nhiên, cụ thể đối với các trường hợp bạn biết các số là & nbsp; không phải & nbsp; bằng nhau, bạn có thể biết rằng & nbsp; ____ ____ 266 & nbsp; cũng sẽ trả về & nbsp; ________ 60. Mặc dù ví dụ này là chính xác, nhưng nó không phải là một ví dụ về phong cách mã hóa Python tốt.

>>>

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

Bạn có thể thấy ở trên rằng & nbsp; ________ 357 & nbsp; returns & nbsp; ________ 58, như đối với bất kỳ giá trị nào. Điều này có nghĩa là & nbsp; ________ 370 & nbsp; giống như & nbsp; ________ 371. Booleans là các loại số và & nbsp; ________ 58 & nbsp; bằng & nbsp; ________ 92. Vì vậy, & nbsp; ________ 371 & nbsp; giống như & nbsp; ________ 375. Vì đây là A & nbsp; nghiêm ngặt & nbsp; bất bình đẳng và & nbsp; ________ 376, nó trả về sai.

Biểu thức thứ hai hoạt động khác nhau:

>>>

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

Vì & nbsp; ________ 94 & nbsp; ít hơn & nbsp; ________ 92, & nbsp; ________ 359 & nbsp; returns & nbsp; ________ 58. Vì & nbsp; ________ 381, thì nó có thể là trường hợp mà & nbsp; ________ 382.

Lưu ý: Don Tiết lấy các phần trên & nbsp; ________ 383 & nbsp; nhẹ. Sử dụng & nbsp; ________ 266 & nbsp; trên các số có thể là & nbsp; khó hiểu. Tuy nhiên, cụ thể đối với các trường hợp bạn biết các số là & nbsp; không phải & nbsp; bằng nhau, bạn có thể biết rằng & nbsp; ____ ____ 266 & nbsp; cũng sẽ trả về & nbsp; ________ 60. Mặc dù ví dụ này là chính xác, nhưng nó không phải là một ví dụ về phong cách mã hóa Python tốt.

Bài học quan trọng nhất để rút ra từ điều này là việc so sánh chuỗi với & nbsp; ________ 266 & nbsp; thường là một ý tưởng tốt. Nó nhầm lẫn người đọc và có lẽ là cần thiết.

>>>

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

Như & nbsp; ________ 266, & nbsp; ________ 282 & nbsp; nhà điều hành và đối diện của nó, & nbsp; ____ 307, thường có thể mang lại kết quả đáng ngạc nhiên khi bị xích:

Để GIảM THIểU Sự NHầM LẫN, VI MÃ ĐượC VIếT TốT! & NBSP;

Cuối Cùng, Bạn Có Thể Xâu Chuỗi & Nbsp;

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
72và 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
07. Đ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 & nbsp;

>>>

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

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
00chỉ đượ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 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
98. Các giá trị mà 
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
98coi 
>>> 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à & nbsp; ________ 398Coi & nbsp;

>>> False = 5 File "", line 1 SyntaxError: cannot assign to False 98quyế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 08. Bạn đã gặp phải >>> True == 1 True >>> False == 0 True >>> True + (False / True) 1.0 08kiểu Boolean trong Python. Khi được gọi, nó chuyển đổi các đối tượng thành Booleans.

Không & nbsp; dưới dạng giá trị boolean

>>>

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

Đối tượng singleton & nbsp;

>>>

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

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

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
98câ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
12. Đôi khi 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
10có thể hữu ích khi kết hợp với ĐổiH Giá ngắn Mạch Để nhập Giá Trị MặC ĐịNH.

Ví dụ:

>>>

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

Trong ví dụ nào, Danh Sách Sẽ Không Đan tạo nếu & nbsp; ________ 416là

Các số dưới dạng giá trị boolean

>>>

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

Như với số nguyên và số dấu phẩy động, phân số chỉ sai khi chú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.

Các 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
31mô-đun cũng là trong thư viện chuẩn. Các số thập phân chỉ giả mạo tương tự khi chú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:

>>>

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

Con số 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
33là một xấp xỉ của Pi đến hai chữ số thập phân. Thực tế này đã được Archimedes thảo luận vào thế kỷ thứ 3 trước Công nguyên . Khi sự khác biệt giữa 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
33và Pi được tính với độ chính xác này, kết quả là sai. Khi sự khác biệt được tính toán với độ chính xác cao hơn, sự khác biệt 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, và sự thật cũng vậy.

Chuỗi dưới dạng giá trị Boolean

Nói chung, các đối tượng có 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
36ý muốn là sai khi kết quả của 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
36là 
>>> 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. Không quan trọng nếu chúng là danh sách, bộ giá trị, bộ, chuỗi hoặc chuỗi byte:

>>>

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

Tất cả các đối tượng Python dựng sẵn có độ dài đều tuân theo quy tắc này. Sau đó, bạn sẽ thấy một số ngoại lệ đối với quy tắc này đối với các đối tượng không được tạo sẵn.

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
36hoặ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

Đ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:

>>>

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

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
40:

>>>

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

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

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

>>>

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

Câu 

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
98lệnh cũng sử dụng 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
40. 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.

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

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
44phươ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
36. 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:

>>>

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

Trong ví dụ này, 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
47sẽ 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
49sau đó. 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
40không cung cấp độ dài cho các trường hợp:

>>>

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

Định nghĩa 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
40khô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
36. Khi cả hai 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
40và 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
54được xác định, 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
40ưu tiên:

>>>

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

Mặc dù 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69có chiều dài 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
57, 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:

>>>

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

Mặc dù 

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
69có độ 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
0

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
1

Mặc dù các mảng trống hiện đang là sai , nhưng việc dựa vào hành vi này là rất nguy hiểm. Trong một số phiên bản NumPy trong tương lai, điều này sẽ đưa ra một ngoại lệ.

Toán tử và Chức năng

Có một vài nơi khác trong Python nơi thử nghiệm Boolean diễn ra. Một trong số đó là trong toán tử Boolean.

Các nhà khai thác 

>>> bool

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

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30chấp nhận bất kỳ giá trị mà hỗ trợ thử nghiệm Boolean. Trong trường hợp của 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30, nó sẽ luôn 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
2

Bảng sự thật cho 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30vẫn đúng, nhưng bây giờ nó có tính xác thực của đầu vào.

Trong trường hợp 

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92, ngoài việc đánh giá ngắn mạch, chúng cũng trả về giá trị mà chúng đã ngừng đá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
3

Bảng sự thật cho 

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30vẫn đúng, nhưng bây giờ nó có tính xác thực của đầu vào.

Trong trường hợp 

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92, ngoài việc đánh giá ngắn mạch, chúng cũng trả về giá trị mà chúng đã ngừng đánh giá:

Các bảng sự thật vẫn đúng, nhưng giờ đây chúng xác định độ tin cậy của các kết quả, điều này phụ thuộc vào độ tin cậy của các đầu vào. Điều này có thể hữu ích khi bạn muốn đặt giá trị mặc định.

>>>

Bảng sự thật cho 
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
30vẫn đúng, nhưng bây giờ nó có tính xác thực của đầu vào.

Trong trường hợp 

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
92, ngoài việc đánh giá ngắn mạch, chúng cũng trả về giá trị mà chúng đã ngừng đánh giá:

Các bảng sự thật vẫn đúng, nhưng giờ đây chúng xác định độ tin cậy của các kết quả, điều này phụ thuộc vào độ tin cậy của các đầu vào. Điều này có thể hữu ích khi bạn muốn đặt giá trị mặc định.

>>>

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

Giả sử bạn có một hàm được gọi là 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
70, nếu văn bản quá dài, hãy lấy đầu và cuối và thêm dấu chấm lửng ( 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
71) ở giữa. Điều này có thể hữu ích trong một số báo cáo không thể phù hợp với toàn văn. Tuy nhiên, một số tập dữ liệu có các giá trị bị thiếu được đại diện bởi 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
10.

Vì 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
70giả sử đầu vào là một chuỗi, nó sẽ không thành công trên 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
10:

>>>

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

>>> 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 10và thực tế là >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 92khô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 77vào đối số >>> True == 1 True >>> False == 0 True >>> True + (False / True) 1.0 70. Việc bổ sung >>> True == 1 True >>> False == 0 True >>> True + (False / True) 1.0 77nà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
80và 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
81đá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
80kiểm tra xem tất cả các đối số của nó có trung thực hay không:

Trong dòng cuối cùng, 

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
80không đánh giá 
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
84cho 
>>> 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
86là 
>>> 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
47.

  • >>> True == 1
    True
    >>> False == 0
    True
    >>> True + (False / True)
    1.0
    
    81 kiểm tra xem bất kỳ đối số nào của nó là trung thực hay không:
  • Trong dòng cuối cùng, 
    >>> True == 1
    True
    >>> False == 0
    True
    >>> True + (False / True)
    1.0
    
    81không đánh giá 
    >>> True == 1
    True
    >>> False == 0
    True
    >>> True + (False / True)
    1.0
    
    91cho 
    >>> 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'
    
    57, 
    >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    92, 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    82, 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    66, 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    43, và 
    >>> a_true_alias = True
    >>> a_true_alias
    True
    >>> True = 5
      File "", line 1
    SyntaxError: cannot assign to True
    
    44để 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 
    >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    98câ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 Prev: Python: Các kiểu dữ liệu cơ bản trong Python
Sử dụng Booleans để viết mã Python hiệu quả và dễ đọc Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp