Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Python Booleans: Tận dụng các giá trị của sự thật This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Booleans: Leveraging the Values of Truth
Loại Python Boolean là một trong những loại dữ liệu tích hợp Python. Nó được sử dụng để đại diện cho giá trị sự thật 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"inline.lower()forlineinlines)/len(lines)0.5
7 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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/len(lines)0.5
9 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Hiểu cách các giá trị Boolean hoạt động của Python là quan trọng đối với việc lập trình tốt trong Python.Python Boolean type is one of Python’s built-in data types. It’s
used to represent the truth value of an expression. For example, the expression
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
7 is
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8, while the expression
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
9 is
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Understanding how Python Boolean values behave is important to programming well in Python.
Trong hướng dẫn này, bạn sẽ học cách:
Thao tác các giá trị Boolean với các toán tử BooleanBoolean operators
Chuyển đổi Booleans sang các loại khácother types
Chuyển đổi các loại khác thành Python BooleansPython Booleans
Sử dụng Python Booleans để viết mã Python hiệu quả và có thể đọc đượcefficient and readable Python code
Loại Boolean Python
Loại Boolean Python chỉ có hai giá trị có thể:
>>> lines="""\... He took 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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0
Không có giá trị nào khác sẽ có
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3 như loại của nó. Bạn có thể kiểm tra loại
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 vớ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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Loại
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3 được tích hợp, có nghĩa là nó luôn luôn có sẵn trong Python và không cần phải nhập. Tuy nhiên, chính cái tên này không phải là một từ khóa trong ngôn ngữ. Mặc dù sau đây được coi là kiểu xấu, nhưng nó có thể gán cho tên
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3:built in, meaning it’s always available in Python and doesn’t need to be imported. However, the name itself isn’t a keyword in the language. While the following is considered bad style, it’s possible to assign to the name
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Loại >>> lines = """\
... He took his vorpal sword in hand;
... Long time the manxome foe he sought—
... So rested he by the Tumtum tree
... And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3 được tích hợp, có nghĩa là nó luôn luôn có sẵn trong Python và không cần phải nhập. Tuy nhiên, chính cái tên này không phải là một từ khóa trong ngôn ngữ. Mặc dù sau đây được coi là kiểu xấu, nhưng nó có thể gán cho tên >>> lines = """\
... He took his vorpal sword in hand;
... Long time the manxome foe he sought—
... So rested he by the Tumtum tree
... And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3:
Mặc dù về mặt kỹ thuật có thể, để tránh sự nhầm lẫn, nó rất khuyến khích bạn không nên gán một giá trị khác 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.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Python Booleans làm từ khóakeywords. Unlike many other Python keywords,
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 are Python expressions. Since they’re expressions, they can be used wherever other expressions,
like
Tên tích hợp aren từ khóa. Theo như ngôn ngữ Python, chúng là các biến thường xuyên. Nếu bạn gán cho họ, thì bạn sẽ ghi đè giá trị tích hợp.
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Loại >>> lines = """\
... He took his vorpal sword in hand;
... Long time the manxome foe he sought—
... So rested he by the Tumtum tree
... And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3 được tích hợp, có nghĩa là nó luôn luôn có sẵn trong Python và không cần phải nhập. Tuy nhiên, chính cái tên này không phải là một từ khóa trong ngôn ngữ. Mặc dù sau đây được coi là kiểu xấu, nhưng nó có thể gán cho tên >>> lines = """\
... He took his vorpal sword in hand;
... Long time the manxome foe he sought—
... So rested he by the Tumtum tree
... And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3:
Mặc dù về mặt kỹ thuật có thể, để tránh sự nhầm lẫn, nó rất khuyến khích bạn không nên gán một giá trị khác 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.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.numeric type in Python. This means they’re numbers for all intents and purposes. In other words, you can apply arithmetic operations to Booleans, and you can also compare them to numbers:
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Loại
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3 được tích hợp, có nghĩa là nó luôn luôn có sẵn trong Python và không cần phải nhập. Tuy nhiên, chính cái tên này không phải là một từ khóa trong ngôn ngữ. Mặc dù sau đây được coi là kiểu xấu, nhưng nó có thể gán cho tên
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3:
>>>
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Loại
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3 được tích hợp, có nghĩa là nó luôn luôn có sẵn trong Python và không cần phải nhập. Tuy nhiên, chính cái tên này không phải là một từ khóa trong ngôn ngữ. Mặc dù sau đây được coi là kiểu xấu, nhưng nó có thể gán cho tên
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3:
>>>
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
6 của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Loại
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3 được tích hợp, có nghĩa là nó luôn luôn có sẵn trong Python và không cần phải nhập. Tuy nhiên, chính cái tên này không phải là một từ khóa trong ngôn ngữ. Mặc dù sau đây được coi là kiểu xấu, nhưng nó có thể gán cho tên
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3:
Mặc dù về mặt kỹ thuật có thể, để tránh sự nhầm lẫn, nó rất khuyến khích bạn không nên gán một giá trị khác 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.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3.
Các nhà khai thác Boolean
Các nhà khai thác Boolean là những người nhận đầu vào Boolean và kết quả Boolean trả lại.Boolean inputs and return Boolean results.
Vì các giá trị Python Boolean chỉ có hai tùy chọn có thể,
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8 hoặ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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0, nên có thể chỉ định các toán tử hoàn toàn theo kết quả mà họ gán cho mọi kết hợp đầu vào có thể. 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.truth tables since they’re displayed in a table.
Như bạn sẽ thấy sau, trong một số tình huống, việc biết một đầu vào cho 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á. Điều này được gọi là đánh giá ngắn mạch.short-circuit evaluation.
Tầm quan trọng của đánh giá ngắn mạch phụ thuộc vào 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 nó sẽ chuyên sâu về mặt tính toán để đá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 các trường hợp cực đoan nhất, tính chính xác của mã của bạn có thể xoay quanh việc đánh giá ngắn mạch.
Người vận hành 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 với tư cách là các nhà khai thác Boolean không có đầu vào. Một trong những nhà khai thác 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"inline.lower()forlineinlines)/len(lines)0.5
8 và người còn lại 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.
Suy nghĩ về các giá trị Boolean Python là người vận hành đô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 họ không phải là biến. Vì lý do tương tự, bạn có thể gán cho
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
25, nó 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"inline.lower()forlineinlines)/len(lines)0.5
8 hoặ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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.
Chỉ có hai giá trị Boolean Python tồn tại. Một 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 là hai nhà khai thác boolean duy nhất mà không cần phải đầu vào.
Nhà điều hành boolean >>> bool
<class 'bool'>
>>> bool = "this is not a type"
>>> bool
'this is not a type'
30
Toán tử boolean duy nhất có một đối số là
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30. Nó lấy 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 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"inline.lower()forlineinlines)/len(lines)0.5
8 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"inline.lower()forlineinlines)/len(lines)0.5
8 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.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Đây là trong một bảng sự thật:
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
36
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
37
>>> lines="""\... He took 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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0
Bảng này minh họa rằng
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 trả về giá trị sự thật ngược lại của đối số. Vì
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 chỉ 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ả của nó:
>>> bool<class '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ả 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
36
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
37
Bạn có thể tự hỏi tại sao không có nhà khai thác boolean nào khác có một đối số. Để hiểu lý do tại sao, bạn có thể nhìn vào một bảng hiển thị tất cả các toán tử boolean có thể về mặt lý thuyết sẽ có một đối số:
Xác thực
Đú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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0
Bảng này minh họa rằng
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 trả về giá trị sự thật ngược lại của đối số. Vì
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 chỉ 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ả của nó:
>>>: Since this operator simply returns its input, you could just delete it from your code with no effect.
Dòng cuối cùng cho thấy
>>> bool<class '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ả 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.: This is a short-circuit operator since it doesn’t depend on its argument. You could just replace it with
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8 and get the same result.
Bạn có thể tự hỏi tại sao không có nhà khai thác boolean nào khác có một đối số. Để hiểu lý do tại sao, bạn có thể nhìn vào một bảng hiển thị tất cả các toán tử boolean có thể về mặt lý thuyết sẽ có một đối số:: This is another short-circuit operator since it doesn’t depend on its argument. You could just replace it with
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 and get the same result.
Xác thực
Đúng
Không
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
36
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
70
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
71
>>> lines="""\... He took 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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0
Bảng này minh họa rằng
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 trả về giá trị sự thật ngược lại của đối số. Vì
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 chỉ 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ả của nó:
>>>
Bảng này minh họa rằng
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 trả về giá trị sự thật ngược lại của đối số. Vì
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 chỉ 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ả của nó:
>>> defprint_and_return(x):... print(f"I am returning {x}")... returnx...>>> Trueandprint_and_return(True)I am returning TrueTrue>>> Trueandprint_and_return(False)I am returning FalseFalse>>> Falseandprint_and_return(True)False>>> Falseandprint_and_return(False)False
>>>
Dòng cuối cùng cho thấy
>>> bool<class '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ả 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.
>>>
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
Hàm
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
91 được thừa nhận là ngớ ngẩn, và nhiều lớp lót sẽ cảnh báo về biểu thức
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
92 là vô dụng. Nó phục vụ mục đích thất bại gọn gàng khi được cho
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 dưới dạng tham số kể từ khi phân chia bởi
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 là không hợp lệ. Tuy nhiên, dòng cuối cùng không làm tăng một ngoại lệ. Do đánh giá ngắn mạch, chức năng được gọi là, sự phân chia bởi
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 không xảy ra, và không có ngoại lệ nào được nêu ra.
Ngược lại,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
96 sẽ tăng một ngoại lệ. Trong trường hợp đó, giá trị của đầu vào thứ hai sẽ là cần thiết cho kết quả của
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64. Khi đầu vào thứ hai được đánh giá,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
98 sẽ được gọi, nó sẽ chia cho
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 và một ngoại lệ sẽ được nâng lên.
Nhà điều hành boolean >>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
File "<stdin>", line 1
SyntaxError: cannot assign to True
00
Giá trị của toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 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"inline.lower()forlineinlines)/len(lines)0.5
8 trừ khi cả hai đầu vào của 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 cũng có thể được xác định bằng bảng sự thật sau:
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
36
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
70
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
07
>>> lines="""\... He took 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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0
Bảng này là dài dòng, nhưng nó có cùng ý nghĩa với lời giải thích ở trên.
Khi được sử dụng một cách không chính thức, từ hoặc có thể có một trong hai ý nghĩa:
Độc quyền hoặc là cách hoặc được sử dụng trong cụm từ Bạn có thể nộp cho một phần mở rộng hoặc gửi bài tập về nhà của bạn đúng hạn. Trong trường hợp này, bạn có thể cả hai tệp cho một phần mở rộng và gửi bài tập về nhà đúng hạn.exclusive or is how or is used in the phrase “You can file for an extension or submit your homework on time.” In this case, you can’t both file for an extension and submit your
homework on time.
Bao gồm hoặc đôi khi được chỉ định bằng cách sử dụng kết hợp và/hoặc. Ví dụ, nếu bạn làm tốt nhiệm vụ này, thì bạn có thể được tăng lương và/hoặc một chương trình khuyến mãi có nghĩa là bạn có thể được tăng lương và khuyến mãi.inclusive or is sometimes indicated by using the conjunction and/or. For example, “If you do well on this task, then you can get a raise and/or a promotion” means that you might get both a raise and a promotion.
Khi Python diễn giải từ khóa
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00, nó sẽ sử dụng bao gồm hoặc. Nếu cả hai đầu vào 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"inline.lower()forlineinlines)/len(lines)0.5
8, thì kết quả của
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 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"inline.lower()forlineinlines)/len(lines)0.5
8.
Bởi vì nó sử dụng một toán tử bao gồm hoặc,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 trong Python cũng sử dụng đánh giá ngắn mạch. Nếu đối số đầ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.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8, thì kết quả là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8 và không cần phải đánh giá đối số thứ hai. Các ví dụ sau đây cho thấy đánh giá ngắn mạch của
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
0
Đầu vào thứ hai được đánh giá bởi
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 trừ khi đầ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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Trong thực tế, việc đánh giá ngắn mạch của
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 được sử dụng ít thường xuyên hơn so với
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64. Tuy nhiên, điều quan trọng là phải ghi nhớ hành vi này khi đọc mã.
Các nhà khai thác Boolean khác
Lý thuyết toán học của logic Boolean xác định rằng không có nhà khai thác nào khác ngoài
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 là cần thiết. Tất cả các toán tử khác trên hai đầu vào có thể được chỉ định theo ba toán tử này. Tất cả các toán tử trên ba hoặc nhiều đầu vào có thể được chỉ định theo các toán tử của hai đầu vào.
Trên thực tế, ngay cả có cả
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 và
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 là dư thừa. Toán tử
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 có thể được xác định theo các điều khoản
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 và toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 có thể được xác định theo các điều khoản của
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30 và
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64. Tuy nhiên,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 rất hữu ích đến nỗi 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ể. Ngoại trừ
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00, 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"inline.lower()forlineinlines)/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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00 là các nhà khai thác Python Boolean tích hợp duy nhất.
Toán tử so sánh
Một số nhà khai thác Python, kiểm tra xem mối quan hệ có giữ giữa hai đối tượng hay không. Vì mối quan hệ hoặc giữ hoặc không giữ được, các toán tử này, được gọi là toán tử so sánh, luôn trả lại các giá trị boolean.comparison operators, always return Boolean values.
Các toán tử so sánh là nguồn giá trị boolean phổ biến nhất.
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_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
52) và toán tử bất bình đẳng (
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
53). Nó gần như không thể viết bất kỳ số lượng mã python có ý nghĩa nào mà không sử dụng ít nhất một trong những nhà khai thác đó.equality operator (
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
52) and the inequality operator (
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
53). It’s almost impossible to write any meaningful amount of Python code without using at least one of those operators.
Toán tử bình đẳng (
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
52) 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 kết quả đã biết hoặc hai kết quả chưa biết với nhau. Một số chức năng trả về các giá trị cần được so sánh với Sentinel để xem liệu một số điều kiện cạnh đã được phát hiện. Đôi khi bạn cần so sánh kết quả từ hai chức năng với nhau.
Toán tử bình đẳng thường được sử dụng để so sánh các số:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
1
Bạn có thể đã sử dụng các toán tử bình đẳng trước đây. Họ là một số nhà khai thác phổ biến nhất trong Python. Đối với tất cả các đối tượng Python tích hợp và đối với hầu hết các lớp của bên thứ ba, chúng sẽ 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"inline.lower()forlineinlines)/len(lines)0.5
8 hoặ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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.Boolean value:
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8 or
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.
Thứ hai chỉ cho toán tử bình đẳng phổ biến là toán tử bất bình đẳng (
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
53). 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"inline.lower()forlineinlines)/len(lines)0.5
8 nế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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 nếu chúng có. Các ví dụ có phạm vi rộng tương tự. Nhiều bài kiểm tra đơn vị kiểm tra xem giá trị có bằng với một giá trị không hợp lệ cụ thể không. Một máy khách web có thể kiểm tra xem mã lỗi không phải là
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
60 trước khi thử thay thế.inequality operator
(
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
53). It returns
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8 if the arguments aren’t equal 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 if they are. The examples are similarly wide-ranging. Many unit tests check that the value isn’t equal to a specific invalid value. A web client might check that the error code isn’t
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
60 before trying an alternative.
Dưới đây là hai ví dụ về toán tử bất bình đẳng Python đang sử dụng:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
2
Có lẽ điều đáng ngạc nhiên nhất về nhà điều hành bất bình đẳng Python là thực tế là nó tồn tại ở nơi đầu tiên. Rốt cuộc, bạn có thể đạt được kết quả tương tự như
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
61 với
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
62. Python thường tránh cú pháp thêm, và đặc biệt là các toán tử cốt lõi, cho những 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 nó được coi là đáng để có một nhà điều hành chuyên dụng cho nó. Trong các phiên bản cũ của Python, trong sê -ri
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
63, thực sự có hai cú pháp khác nhau.
Là một trò đùa 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 nhập khẩu đúng
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
64:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
3
Có lẽ điều đáng ngạc nhiên nhất về nhà điều hành bất bình đẳng Python là thực tế là nó tồn tại ở nơi đầu tiên. Rốt cuộc, bạn có thể đạt được kết quả tương tự như
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
61 với
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
62. Python thường tránh cú pháp thêm, và đặc biệt là các toán tử cốt lõi, cho những 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 nó được coi là đáng để có một nhà điều hành chuyên dụng cho nó. Trong các phiên bản cũ của Python, trong sê -ri >>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
File "<stdin>", line 1
SyntaxError: cannot assign to True
63, thực sự có hai cú pháp khác nhau.
Là một trò đùa 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 nhập khẩu đúng
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
64:order comparison operators. There are four order comparison operators that can be categorized by two qualities:
Điều này không bao giờ nên được sử dụng trong bất kỳ mã nào có nghĩa là để sử dụng thực sự. Tuy nhiên, nó có thể có ích cho đêm Python tiếp theo của bạn.: Is it less than or greater than?
So sánh đặt hàng: Is equality allowed or not?
Một bộ 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 phẩm chất:
Hướng: Nó ít hơn hoặc lớn hơn?
Sự nghiêm ngặt: Bình đẳng có được phép hay không?
Vì hai lựa chọn là độc lập, bạn nhận được các nhà khai thác so sánh đơn đặt hàng
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
65. Tất cả bốn người được liệt kê trong bảng này:
Ít hơn
Lớn hơn
Nghiêm khắc
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
66
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
67
Không nghiêm khắc
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
68lexicographically,
dictionaries don’t have a meaningful order:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
4
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
69
Có hai lựa chọn cho hướng và hai tùy chọn cho sự nghiêm ngặt. Điều này dẫn đến tổng số bốn nhà khai thác so sánh đơn hàng.
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
5
Các toán tử so sánh đơn hàng aren được xác định cho tất cả các đối tượng. Một số đối tượng don lồng có một trật tự có ý nghĩa. Mặc dù các danh sách và bộ dữ liệu được đặt hàng từ vựng, từ điển don don có một thứ tự có ý nghĩa:
Nó không rõ ràng làm thế nào từ điển nên được đặt hàng. Theo Zen of Python, trước sự mơ hồ, Python từ chối đoán.
Trong khi các chuỗi và số nguyên được đặt hàng riêng biệt, các so sánh giữa các loại được hỗ trợ:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
6
Một lần nữa, vì không có cách nào rõ ràng để xác định trật tự, Python từ chối so sánh chúng. Điều này tương tự như toán tử bổ sung (
>>> bool<class '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ẽ làm tăng một ngoại lệ.
Khi các toán tử so sánh đơn hàng được xác định, nói chung, họ trả lại một boolean.
So sánh các con số trong Python là một cách phổ biến để kiểm tra chống lại các điều kiện biên. Lưu ý rằng
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
66 không cho phép bình đẳng, trong khi
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
68 không:object identity. In other words,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
75 evaluates to
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8 only when
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
77 and
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
78 evaluate to the same object. The
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 operator has an
opposite, the
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
80 operator.
Các lập trình viên thường sử dụng các toán tử so sánh mà không nhận ra rằng họ trả lại giá trị Boolean Python.
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
7
Toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73
Toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 kiểm tra nhận dạng đối tượng. Nói cách khác,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
75 chỉ đánh giá thà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"inline.lower()forlineinlines)/len(lines)0.5
8 khi
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
77 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
78 đánh giá thành cùng một đối tượng. Toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 có một toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
80.
Cách sử dụng điển hình của >>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
File "<stdin>", line 1
SyntaxError: cannot assign to True
73 và >>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
File "<stdin>", line 1
SyntaxError: cannot assign to True
80 là so sánh danh sách cho danh tính:
Mặc dù
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
83, chúng không phải là cùng một đối tượng. Toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
80 luôn trả về đối diện với
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73. Không có sự khác biệt giữa biểu thức
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
86 và biểu thức
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
87 ngoại trừ khả năng đọc.membership. An object can define what it considers members. Most sequences, such as lists, consider their elements to be members:
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
8
Hãy nhớ rằng các ví dụ trên hiển thị toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 chỉ được sử dụng với danh sách. Hành vi của toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 trên các đối tượng bất biến như số và chuỗi phức tạp hơn.
Toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90
>>>
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
9
Nhà điều hành
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90 kiểm tra tư cách thành viên. Một đối tượng có thể xác định những gì nó xem xét các thành viên. Hầu hết các chuỗi, chẳng hạn như danh sách, coi các yếu tố của họ là thành viên:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
0
Vì
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
06 là một chuỗi con, toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90 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"inline.lower()forlineinlines)/len(lines)0.5
8. Vì
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
09 không phải là một chuỗi con, toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Điều này mặc dù thực tế là mỗi chữ cái riêng lẻ trong
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
09 là một thành viên của chuỗi.
Giống như các toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
52, toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90 cũng có một điều ngược lại,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
16. Bạn có thể sử dụng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
16 để xác nhận rằng một phần tử không phải là thành viên của một đối tượng.
Chuỗi các nhà khai thác so sánh
Các nhà khai thác so sánh có thể hình thành chuỗi. Bạn có thể tạo chuỗi toán tử so sánh bằng cách tách các biểu thức với các toán tử so sánh để tạo thành một biểu thức lớn hơn:chains. You can create comparison operator chains by separating expressions with comparison operators to form a larger expression:
Biểu thức
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
18 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à
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8 vì cả hai phần của chuỗi là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8. Bạn có thể phá vỡ chuỗi để xem nó hoạt động như thế nào:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
1
Kể từ khi
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
21 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"inline.lower()forlineinlines)/len(lines)0.5
8 và
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
23 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"inline.lower()forlineinlines)/len(lines)0.5
8,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 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"inline.lower()forlineinlines)/len(lines)0.5
8. Một chuỗi so sánh tương đương với việc sử dụng
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 trên tất cả các liên kết của nó. Trong trường hợp này, kể từ
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
28 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"inline.lower()forlineinlines)/len(lines)0.5
8, kết quả của toàn bộ chuỗi là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8. Điều này có nghĩa là nếu bất kỳ liên kết nào 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0, thì toàn bộ chuỗi là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0:
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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 vì không phải tất cả các liên kết của 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.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8. Bởi vì chuỗi so sánh là một toán tử
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 ngầm, nếu thậm chí 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0, thì toàn bộ chuỗi là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Bạn có thể phá vỡ chuỗi để xem nó hoạt động như thế nào:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
2
Trong trường hợp này, các phần của chuỗi đánh giá các booleans sau:
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
38 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"inline.lower()forlineinlines)/len(lines)0.5
8
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
40 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0
Điều này có nghĩa là một trong các kết quả là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8 và một là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0. Vì
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
44 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0, giá trị của toàn bộ chuỗi là
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0.
Bạn có thể trộn các loại và hoạt động trong chuỗi so sánh miễn là các loại có thể được so sánh:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
3
Các nhà khai thác don lồng phải giống nhau. Thậm chí không phải các loại phải giống nhau. Trong các ví dụ trên, bạn có ba loại số:
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
47
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
48
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
3
Đây là ba loại số khác nhau, nhưng bạn có thể so sánh các đối tượng thuộc các loại số khác nhau mà không gặp vấn đề gì.
Đánh giá chuỗi ngắn mạch
Nếu chuỗi sử dụng
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 ngầm, thì chuỗi cũng phải ngắn mạch. Điều này rất quan trọng bởi vì ngay cả trong trường hợp so sánh đơn đặt hàng được xác định, thì một chuỗi có thể trả 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
4
Mặc dù Python có thể đặt hàng số nguyên và số chuỗi,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
52 đánh giá thà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.... """>>> line_list=lines.splitlines()>>> "the"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 vì 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: tăng một ngoại lệ.
Đánh giá ngắn mạch các chuỗi so sánh có thể ngăn chặn các trường hợp ngoại lệ khác:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
5
Chia
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2 cho
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 sẽ tăng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
56. Tuy nhiên, do đá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 đánh giá không chỉ so sánh mà còn cả các đầu vào để so sánh.
Một khía cạnh khác quan trọng để hiểu về các chuỗi so sánh là khi Python đánh giá một yếu tố trong chuỗi, nó chỉ đánh giá nó một lần:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
6
Bởi vì các yếu tố giữa chỉ được đánh giá một lần, nên không phải lúc nào cũng an toàn để tái cấu trúc
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
57 thành
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
58. Mặc dù chuỗi hoạt động như
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64 trong đánh giá ngắn mạch của nó, nhưng nó đánh giá tất cả các giá trị, bao gồm cả các giá trị trung gian, chỉ một lần.
Các chuỗi đặc biệt hữu ích cho 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:range checks, which confirm that a value falls within a given range. For example, in a daily invoice that includes the number hours worked, you might do the following:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
7
Nếu có
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 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à
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
61. 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.
Trộn các nhà khai thác và chuỗi
Cho đến bây giờ, tất cả các ví dụ của chúng tôi liên quan đến
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
52,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
53 và so sánh thứ tự. Tuy nhiên, bạn có thể xâu chuỗi tất cả các nhà khai thác so sánh Python. Điều này có thể dẫn đến hành vi đáng ngạc nhiên:
>>>
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
8
Bởi vì
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
64 là một chuỗi so sánh, nó đánh giá thà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"inline.lower()forlineinlines)/len(lines)0.5
8. Bạn có thể chia chuỗi thành các phần của nó:
Biểu thức
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
66 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"inline.lower()forlineinlines)/len(lines)0.5
8, vì nó sẽ dành cho bất kỳ giá trị nào được đánh giá chống lại chính nó.
Biểu thức
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
68 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"inline.lower()forlineinlines)/len(lines)0.5
8 vì
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 nhỏ hơn
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2.
Vì cả hai phầ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.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8, chuỗi đánh giá thà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"inline.lower()forlineinlines)/len(lines)0.5
8.
Tuy nhiên, những người quen với các nhà khai thác khác trong Python có thể cho rằng, giống như các biểu thức khác liên quan đến nhiều toán tử như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
74, Python chèn dấu ngoặc đơn vào biểu thức. Tuy nhiên, không có cách nào chèn dấu ngoặc đơn sẽ đánh giá thà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"inline.lower()forlineinlines)/len(lines)0.5
8.
Bạn có thể thấy tại sao cả hai đánh giá đế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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 nếu bạn chia nhỏ 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_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
9
Bạn có thể thấy ở trên rằng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
66 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"inline.lower()forlineinlines)/len(lines)0.5
8, vì nó sẽ cho bất kỳ giá trị nào. Điều này có nghĩa là
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
79 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80. Booleans là loại số 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"inline.lower()forlineinlines)/len(lines)0.5
8 bằng
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2. Vì vậy,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
84. Vì đây là một bất bình đẳng nghiêm ngặt và
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
85, nó trả về sai.strict inequality, and
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
85, it returns False.
Biểu thức thứ hai hoạt động khác nhau:
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
0
Bạn có thể thấy ở trên rằng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
66 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"inline.lower()forlineinlines)/len(lines)0.5
8, vì nó sẽ cho bất kỳ giá trị nào. Điều này có nghĩa là
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
79 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80. Booleans là loại số 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"inline.lower()forlineinlines)/len(lines)0.5
8 bằng
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2. Vì vậy,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
84. Vì đây là một bất bình đẳng nghiêm ngặt và
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
85, nó trả về sai.
Biểu thức thứ hai hoạt động khác nhau:
Vì
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 nhỏ hơn
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
68 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"inline.lower()forlineinlines)/len(lines)0.5
8. Kể từ
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
90, thì nó có thể là trường hợp
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
91.
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
1
Bạn có thể thấy ở trên rằng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
66 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"inline.lower()forlineinlines)/len(lines)0.5
8, vì nó sẽ cho bất kỳ giá trị nào. Điều này có nghĩa là
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
79 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80. Booleans là loại số 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"inline.lower()forlineinlines)/len(lines)0.5
8 bằng
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2. Vì vậy,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
84. Vì đây là một bất bình đẳng nghiêm ngặt và
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
85, nó trả về sai.
Biểu thức thứ hai hoạt động khác nhau:
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
2
Bạn có thể thấy ở trên rằng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
66 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"inline.lower()forlineinlines)/len(lines)0.5
8, vì nó sẽ cho bất kỳ giá trị nào. Điều này có nghĩa là
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
79 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80. Booleans là loại số 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"inline.lower()forlineinlines)/len(lines)0.5
8 bằng
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2. Vì vậy,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
84. Vì đây là một bất bình đẳng nghiêm ngặt và
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
85, nó trả về sai.
Biểu thức thứ hai hoạt động khác nhau:
Vì
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4 nhỏ hơn
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
68 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"inline.lower()forlineinlines)/len(lines)0.5
8. Kể từ
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
90, thì nó có thể là trường hợp
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
91.
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
3
Bạn có thể thấy ở trên rằng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
66 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"inline.lower()forlineinlines)/len(lines)0.5
8, vì nó sẽ cho bất kỳ giá trị nào. Điều này có nghĩa là
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
79 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80. Booleans là loại số 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"inline.lower()forlineinlines)/len(lines)0.5
8 bằng
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2. Vì vậy,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
80 giống như
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
84. Vì đây là một bất bình đẳng nghiêm ngặt và
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
0 are called
falsy.
Biểu thức thứ hai hoạt động khác nhau:
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 "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
4 nhỏ hơn >>> def inverse_and_true(n):
... 1 // n
... return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2, >>> False = 5
File "<stdin>", line 1
SyntaxError: cannot assign to False
68 trả về >>> lines="""\
... He took his vorpal sword in hand;
... Long time the manxome foe he sought—
... So rested he by the Tumtum tree
... And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
8. Kể từ >>> False = 5
File "<stdin>", line 1
SyntaxError: cannot assign to False
90, thì nó có thể là trường hợp >>> False = 5
File "<stdin>", line 1
SyntaxError: cannot assign to False
91.
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
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73 thường không phải là một ý tưởng tốt. Nó nhầm lẫn người đọc và có lẽ là cần thiết.
Giống như
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73, toán tử
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90 và đối diện của nó,
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
16, thường có thể mang lại kết quả đáng ngạc nhiên khi bị xích:
Để tối đa hóa sự nhầm lẫn, các chuỗi ví dụ này so sánh với các toán tử khác nhau và sử dụng
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90 với các chuỗi để kiểm tra các chất nền. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt! Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu lý do tại sao nó trả về
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
8.
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
4
Cuối cùng, bạn có thể chuỗi
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
80 với
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
16:
Lưu ý rằng thứ tự của >>> bool
<class 'bool'>
>>> bool = "this is not a type"
>>> bool
'this is not a type'
30 trong hai toán tử không giống nhau! Các toán tử tiêu cực là >>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
File "<stdin>", line 1
SyntaxError: cannot assign to True
80 và >>> False = 5
File "<stdin>", line 1
SyntaxError: cannot assign to False
16. Điều này tương ứng với việc sử dụng thường xuyên bằng tiếng Anh, nhưng nó dễ dàng mắc lỗi khi sửa đổi mã.
Thử nghiệm Python Boolean
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
5
Việc sử dụng phổ biến nhất cho Python Boolean là trong một tuyên bố
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8:
>>>
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
8. Tuy nhiên, trong Python, bạn có thể cung cấp bất kỳ giá trị nào 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"inline.lower()forlineinlines)/len(lines)0.5
>>> lines="""\... He took 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"inline_list[0]False>>> "the"inline_list[1]True>>> 0+False+True# Equivalent to 0 + 0 + 11>>> ["the"inlineforlineinline_list][False, True, True, False]>>> False+True+True+False2>>> len(line_list)4>>> 2/40.5
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4. Nó không quan trọng nếu họ liệt kê danh sách, bộ dữ liệu, bộ, chuỗi hoặc chuỗi byte:
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
42 hoặc xác định cụ thể liệu chúng là sự thật hay giả, họ luôn luôn là sự thật. Điều này đúng với các loại tích hợp cũng như do người dùng xác định. Cụ thể, các chức năng luôn là sự thật:
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Tất cả các đối tượng Python tích hợp có độ dài 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 cho các đối tượng không được xây dựng.
Trừ khi các loại có >>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
42 hoặc xác định cụ thể liệu chúng là sự thật hay giả, họ luôn luôn là sự thật. Điều này đúng với các loại tích hợp cũng như do người dùng xác định. Cụ thể, các chức năng luôn là sự thật:
Phương pháp luôn luôn là sự thật. Bạn có thể gặp phải điều này nếu thiếu dấu ngoặc đơn khi bạn gọi một hàm hoặc phương thức:
Điều này có thể xảy ra do kết quả của một dấu ngoặc đơn bị lãng quên hoặc tài liệu gây hiểu lầm mà không đề cập đến việc bạn cần gọi chức năng. Nếu bạn mong đợi một giá trị Boolean Python nhưng có một hàm trả về giá trị Boolean, thì nó sẽ luôn là sự thật.
Theo mặc định, các loại do người dùng định nghĩa luôn là sự thật:
Tạo một lớp trống làm cho mọi đối tượng của sự thật lớp đó. Tất cả các đối tượng là sự thật trừ khi các phương pháp đặc biệt được xác định. Nếu bạn muốn thực hiện một số trường hợp của lớp Falsy, bạn có thể xá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"inline.lower()forlineinlines)/len(lines)0.5
42. Trong trường hợp đó, giá trị boolean của các trường hợp sẽ sai lệch chính xác khi độ dài của chúng là
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4:
>>>
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
2
Ví dụ trên có vẻ giống như một cái gì đó chỉ xảy ra khi bạn viết một lớp dự định chứng minh các trường hợp cạnh trong Python. Tuy nhiên, 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.
Các mảng, như số, là giả hoặc sự thật tùy thuộc vào cách chúng so sánh với
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4:
>>>
>>> lines="""\... He took his vorpal sword in hand;... Long time the manxome foe he sought—... So rested he by the Tumtum tree... And stood awhile in thought.... """.splitlines()>>> sum("the"inline.lower()forlineinlines)/len(lines)0.5
3
Mặc dù
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
77 có độ dài
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
2, nhưng nó vẫn còn giả vì giá trị của nó là
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4.
Khi các mảng có nhiều hơn một yếu tố, một số yếu tố có thể là giả và một số có thể là sự thật. Trong những trường hợp đó, Numpy sẽ tăng một ngoại lệ:
Ngoại lệ rất dài đến mức để dễ đọc, mã sử dụng xử lý văn bản để bọc các dò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"inline.lower()forlineinlines)/len(lines)0.5
86 Kiểm tra xem tất cả các lập luận của nó là sự thật:
>>>
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4, điều này sẽ tăng
>>> False=5
File "<stdin>", line 1SyntaxError: cannot assign to False
87 Kiểm tra xem bất kỳ đối số nào của nó là sự thật:
>>>
>>> lines="""\... He took 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"inline.lower()forlineinlines)/len(lines)0.5
>>> definverse_and_true(n):... 1//n... returnTrue...>>> inverse_and_true(5)True>>> inverse_and_true(0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in inverse_and_trueZeroDivisionError: integer division or modulo by zero>>> Falseandinverse_and_true(0)False
4.
Sự kết luận
Python Boolean là một loại dữ liệu thường được sử dụng với nhiều ứng dụng hữu ích. Bạn có thể sử dụng Booleans với các nhà khai thác như
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
30,
>>> bool<class 'bool'>>>> bool="this is not a type">>> bool'this is not a type'
64,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
00,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
90,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
73,
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
52 và
>>> a_true_alias=True>>> a_true_aliasTrue>>> True=5
File "<stdin>", line 1SyntaxError: cannot assign to True
53 để so sánh các giá trị và kiểm tra tư cách 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 tuyên bố
03. Kiến thức này sẽ giúp bạn vừa hiểu được mã hiện có và tránh những cạm bẫy phổ biến có thể dẫn đến lỗi trong các chương trình của riêng bạn.
Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Python Booleans: Tận dụng các giá trị của sự thật This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Booleans: Leveraging the Values of Truth
Nó là bool hay boolean trong Python?
Để thể hiện đúng và sai, Python cung cấp cho bạn loại dữ liệu Boolean.Giá trị Boolean có một tên kỹ thuật là Bool.Kiểu dữ liệu Boolean có hai giá trị: Đúng và Sai.Lưu ý rằng các giá trị boolean đúng và sai bắt đầu với các chữ cái viết hoa (t) và (f).The boolean value has a technical name as bool . The boolean data type has two values: True and False . Note that the boolean values True and False start with the capital letters ( T ) and ( F ).
3 nhà khai thác boolean trong Python là gì?
Có ba toán tử logic được sử dụng để so sánh các giá trị.Họ đánh giá các biểu thức xuống các giá trị boolean, trả về đúng hoặc sai.Các toán tử này là và, hoặc, và không và được xác định trong bảng dưới đây.and , or , and not and are defined in the table below.