__Bool__ Trăn

Kiểu Boolean trong Python là một trong những kiểu dữ liệu có sẵn của Python. Nó được sử dụng để đại diện cho giá trị thực 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.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
0 là
>>> lines = """\
.. He took 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
1, 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.
.. """
>>> 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
2 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Hiểu cách các giá trị Boolean của Python hoạt động rất quan trọng để lập trình tốt trong Python

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

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

Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới

Kiểu Boolean của Python

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

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

Không có giá trị nào khác sẽ có loại là

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
6. 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.
.. """
>>> 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
1 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 có tích hợp sẵn
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
9

>>>

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
9 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" 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 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
6

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
7

Mặc dù về mặt kỹ thuật là có thể, nhưng để tránh nhầm lẫn, 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" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
6

Loại bỏ các quảng cáo

Python Booleans làm từ khóa

Tên tích hợp không phải là từ khóa. Đối với ngôn ngữ Python, chúng là các biến thông thường. Nếu bạn gán cho chúng, thì bạn sẽ ghi đè giá trị tích hợp

Ngược lại, 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
1 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 không được tích hợp sẵn. Chúng là từ khóa. Không giống như nhiều từ khóa Python khác,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 là các biểu thức Python. Vì chúng là các biểu thức nên chúng có thể được sử dụng ở bất cứ nơi nào có thể sử dụng các biểu thức khác, chẳng hạn 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" 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
31

Có thể gán giá trị Boolean cho các biến nhưng không thể gán giá trị 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" 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
1

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
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" 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
1 là một từ khóa nên bạn không thể gán giá trị cho nó. Quy tắc tương tự áp dụng 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" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
8

Bạ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.
.. """
>>> 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 vì đó là từ khóa trong Python. Theo cách 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.
.. """
>>> 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
1 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 hoạt động giống như các hằng số khác. Ví dụ: bạn có thể chuyể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
38 cho các hàm hoặc gán nó cho các biến. Tuy nhiên, không thể gán giá trị 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" 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
38. Câu lệ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" 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
40 không hợp lệ Python. Cả
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
40 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
42 đều là mã Python không hợp lệ và sẽ tăng giá trị
>>> lines = """\
.. He took 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
43 khi phân tích cú pháp

Booleans Python dưới dạng số

Booleans được coi là một kiểu số trong Python. Điều này có nghĩa là chúng là những con số cho tất cả ý định và mục đích. Nói cách khác, bạn có thể áp dụng các phép toán số học cho Booleans và bạn cũng có thể so sánh chúng với các số

>>>

>>> lines = """\
.. He took 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
8

Không có nhiều cách sử dụng bản chất số của các giá trị Boolean, nhưng có một kỹ thuật mà bạn có thể thấy hữu ích. Bởi vì

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 bằng 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" 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
45 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 bằng 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" 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
47, cộng các giá trị Boolean lại với nhau là một cách nhanh chóng để đếm số giá trị của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Điều này có thể hữu ích khi bạn cần đếm số mục thỏa mãn một điều kiện

Ví dụ: nếu bạn muốn phân tích một đoạn thơ trong bài thơ cổ điển dành cho trẻ em để xem có bao nhiêu dòng chứa 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.
.. """
>>> 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
49, thì thực tế là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 bằng 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" 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
45 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 bằng 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" 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
47 có thể khá hữu ích

>>>

>>> lines = """\
.. He took 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
9

Tính tổng tất cả các giá trị trong một biểu thức trình tạo như thế này sẽ cho bạn biết số lần

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1 xuất hiện trong trình tạo. Số lần
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1 có trong trình tạo bằng với số dòng có chứa 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.
.. """
>>> 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
49, không phân biệt chữ hoa chữ thường. Chia số này cho tổng số dòng sẽ cho bạn tỷ lệ giữa các dòng phù hợp với tổng số dòng

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

>>>

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

Biế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
707 chứa danh sách các dòng. Dòng đầu tiên không có 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.
.. """
>>> 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
49 trong đó, vì vậy
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
709 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Ở dòng thứ hai,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
49 xuất hiện, vì vậy
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
712 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Vì Booleans là số, bạn có thể thêm chúng vào 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.
.. """
>>> 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
714 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" 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
45

>>> lines = """\
.. He took 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
716 là danh sách bốn Booleans, bạn có thể cộng chúng lại với nhau. Khi bạn thêm
>>> lines = """\
.. He took 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
717, bạn nhận được
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
718. Bây giờ, nếu bạn chia kết quả đó 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" 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
719, độ dài của danh sách, bạn sẽ nhận được
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
720. 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.
.. """
>>> 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
49 xuất hiện ở một nửa dòng trong vùng chọn. Đây là một cách hữu ích để tận dụng lợi thế của thực tế là Booleans là những con số

Toán tử Boolean

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

Ghi chú. Sau này, bạn sẽ thấy rằng các toán tử này có thể được cung cấp các đầu vào khác và không phải lúc nào cũng trả về kết quả Boolean. Hiện tại, tất cả các ví dụ sẽ sử dụng đầu vào và kết quả Boolean. Bạn sẽ thấy cách điều này khái quát hóa các giá trị khác trong phần về tính trung thực

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1 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" 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, nên có thể chỉ định hoàn toàn các toán tử theo kết quả mà chúng 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 chân lý vì chúng được hiển thị trong một bảng

Như bạn sẽ thấy sau này, trong một số tình huống, biết một đầu vào của 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

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

Loại bỏ các quảng cáo

Toán tử không có đầu vào

Bạn có thể coi

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

Đôi khi, nghĩ về các giá trị Boolean của Python dưới dạng toán tử là hữu ích. Ví dụ: cách tiếp cận này giúp nhắc bạn rằng chúng không phải là biến. Vì lý do tương tự, bạ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.
.. """
>>> 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
728, 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.
.. """
>>> 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
1 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" 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

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

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

Toán tử 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.
.. """
>>> 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
733

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733. Nó nhận một đối số và trả về kết quả ngược lại.
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 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" 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
1 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 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" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Đây là một bảng 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.
.. """
>>> 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
739
>>> lines = """\
.. He took 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
740
>>> lines = """\
.. He took 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1

Bảng này minh họa rằng

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733 trả về giá trị chân lý ngược lại của đố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.
.. """
>>> 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
733 chỉ nhận một đối số nên nó không bị đoản mạch. Nó đánh giá đối số của nó trước khi trả về kết quả của 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
4

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

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

Bạn có thể thắc mắc tại sao không có toán tử Boolean nào khác nhận một đối số duy nhất. Để hiểu tại sao, bạn có thể xem bảng hiển thị tất cả các toán tử Boolean có thể về mặt lý thuyết sẽ nhận một đối số

>>> lines = """\
.. He took 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
739
>>> lines = """\
.. He took 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
740Danh tínhCóKhông
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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

Chỉ có bốn toán tử có thể với một đối số. Ngoà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
733, ba toán tử còn lại đều có những cái tên hơi kỳ quái vì chúng không thực sự tồn tại

  • >>> lines = """\
    .. He took 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
    
    762. Vì toán tử này chỉ trả về đầu vào của nó, nên bạn có thể xóa nó khỏi mã của mình mà không ảnh hưởng gì

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

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

Toán tử 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.
.. """
>>> 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
767

Toán 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.
.. """
>>> 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
767 nhận hai đối số. Nó đánh giá là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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 trừ khi 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.
.. """
>>> 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
1. Bạn có thể xác định hành vi của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767 bằng bảng chân lý sau

>>> lines = """\
.. He took 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
739
>>> lines = """\
.. He took 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
773
>>> lines = """\
.. He took 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
774
>>> lines = """\
.. He took 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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

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

>>> lines = """\
.. He took 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
739 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 thì giá trị của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
773 không thành vấn đề. Do đó,
>>> lines = """\
.. He took 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
767 bị đoản mạch nếu đầu vào đầu tiên là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Nói cách khác, nếu đầu vào đầu tiên là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3, thì đầu vào thứ hai không được đánh giá

Đoạn mã sau có đầu vào thứ hai có tác dụng phụ, in, để cung cấp một ví dụ 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.
.. """
>>> 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

Trong hai trường hợp cuối cùng, không có gì được in. Hàm không được gọi vì việc gọi nó không cần thiết để xác định giá trị của toán 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.
.. """
>>> 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
767. Nhận thức về đoản mạch là rất quan trọng khi các biểu hiện có tác dụng phụ. Trong hai ví dụ cuối cùng, việc đánh giá ngắn mạch ngăn tác dụng phụ của quá trình in xảy ra

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
4

Hàm

>>> lines = """\
.. He took 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
794 phải thừa nhận là ngớ ngẩn và nhiều người nói dối sẽ cảnh báo về 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.
.. """
>>> 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
795 là vô dụng. Nó phục vụ mục đích thất bại gọn gàng khi được cung cấp
>>> lines = """\
.. He took 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
47 làm tham số vì phép chia 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" 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
47 không hợp lệ. Tuy nhiên, dòng cuối cùng không đưa ra một ngoại lệ. Do đánh giá ngắn mạch, hàm không được gọi, phép chia 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" 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
47 không xảy ra và không có ngoại lệ nào được đưa ra

Ngược lại,

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
799 sẽ đưa ra một ngoại lệ. Trong trường hợp đó, giá trị của đầu vào thứ hai sẽ cần thiết cho kết quả của
>>> lines = """\
.. He took 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
767. Sau khi đầu vào thứ hai được đánh giá,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
501 sẽ được gọi, nó sẽ chia 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" 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
47 và một ngoại lệ sẽ được đưa ra

Loại bỏ các quảng cáo

Toán tử 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.
.. """
>>> 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
503

Giá trị của toán 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.
.. """
>>> 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
503 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 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" 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. Toán 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.
.. """
>>> 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
503 cũng có thể được định nghĩa bằng bảng chân trị sau

>>> lines = """\
.. He took 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
739
>>> lines = """\
.. He took 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
773
>>> lines = """\
.. He took 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
510
>>> lines = """\
.. He took 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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

Bảng này dài dòng, nhưng nó có ý nghĩa tương tự như giải thích ở trên

Khi được sử dụng 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 thức hoặc được sử dụng trong cụm từ “Bạn có thể nộp đơn xin gia hạn hoặc nộp bài tập về nhà đúng hạn. ” Trong trường hợp này, bạn không thể vừa nộp đơn xin gia hạn vừa nộp bài tập về nhà đúng hạn

  • Bao hàm hoặc đôi khi được biểu thị bằng cách sử dụng liên từ 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 thăng chức” có nghĩa là bạn có thể vừa được tăng lương vừa được thăng chức

Khi Python diễn giải từ khóa

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503, nó sẽ làm như vậy bằng cách sử dụng cụm từ 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.
.. """
>>> 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
1, thì kết quả của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1

Bởi vì nó sử dụng một toán tử bao hàm hoặc, nên toán 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.
.. """
>>> 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
503 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.
.. """
>>> 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
1, 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.
.. """
>>> 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
1 và không cần đánh giá đối số thứ hai. Các ví dụ sau minh họa đánh giá ngắn mạch của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503

>>>

>>> lines = """\
.. He took 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
70

Đầu vào thứ hai không được đánh giá bở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
503 trừ khi đầu vào đầu tiên là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Trong thực tế, đánh giá ngắn mạch của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 ít được sử dụng hơn nhiều so với của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767. Tuy nhiên, điều quan trọng là phải ghi nhớ hành vi này khi đọc mã

Toán tử Boolean khác

Lý thuyết toán học của logic Boolean xác định rằng không cần toán tử nào khác ngoà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
733,
>>> lines = """\
.. He took 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
767 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503. 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ả toán tử trên ba đầu vào trở lên có thể được chỉ định dưới dạng toán tử của hai đầu vào

Trên thực tế, thậm chí có cả

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767 là thừa. Toán 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.
.. """
>>> 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
767 có thể được xác định theo thuật ngữ của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503, và toán 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.
.. """
>>> 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
503 có thể được xác định theo thuật ngữ của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767. Tuy nhiê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
767 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 hữu ích đến mức tất cả các ngôn ngữ lập trình đều có cả hai

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

>>> lines = """\
.. He took 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
767 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503, chúng hiếm khi cần thiết trong thực tế. Do đó,
>>> lines = """\
.. He took 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
1,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
733,
>>> lines = """\
.. He took 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
767 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 là các toán tử Boolean Python tích hợp duy nhất

Toán tử so sánh

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

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

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

Các toán tử so sánh phổ biến nhất là toán tử đẳng 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.
.. """
>>> 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
555] và toán tử bất đẳng 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.
.. """
>>> 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
556]. Hầu 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 các toán tử đó

Toán tử đẳng 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.
.. """
>>> 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
555] là một trong những toán tử được sử dụng nhiều nhất trong mã Python. Bạn thường cần so sánh một kết quả chưa biết với một kết quả đã biết hoặc hai kết quả chưa biết với nhau. Một số hàm trả về các giá trị cần được so sánh với một trọng điểm để xem liệu một số điều kiện cạnh đã được phát hiện hay chưa. Đôi khi bạn cần so sánh kết quả từ hai chức năng với nhau

Toán tử đẳng thức thường được dùng để so sánh các số

>>>

>>> lines = """\
.. He took 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
71

Bạn có thể đã sử dụng các toán tử đẳng thức trước đây. Chúng là một số toán tử phổ biến nhất trong Python. Đối với tất cả các đối tượng Python tích hợp và đối với hầu hết các lớp của bên thứ ba, chúng trả về một 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.
.. """
>>> 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
1 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" 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

Ghi chú. Ngôn ngữ Python không thực thi rằng

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
555 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
556 trả về Booleans. Các thư viện như NumPy và gấu trúc trả về các giá trị khác

Mức độ phổ biến chỉ đứng sau toán tử đẳng thức là toán tử bất đẳng 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.
.. """
>>> 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
556]. Nó trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 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" 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 nếu chúng bằng nhau. 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ị không bằng một giá trị không hợp lệ cụ thể. Một ứng dụng web có thể kiểm tra xem mã lỗi có phải là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
565 không trước khi thử một giải pháp thay thế

Dưới đây là hai ví dụ về toán tử bất đẳng thức Python được sử 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.
.. """
>>> 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
72

Có lẽ điều đáng ngạc nhiên nhất về toán tử bất đẳng thức trong Python là nó tồn tại ngay từ đầu. Rốt cuộc, bạn có thể đạt được kết quả tương tự như

>>> lines = """\
.. He took 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
566 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" 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
567. Python thường tránh cú pháp bổ sung và đặc biệt là các toán tử lõi bổ sung, vì những thứ có thể dễ dàng đạt được bằng các phương tiện khác

Tuy nhiên, bất đẳng thức được sử dụng thường xuyên đến mức cần có một toán tử chuyên dụng cho nó. Trong các phiên bản cũ của Python, trong sê-ri

>>> lines = """\
.. He took 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
568, thực tế có hai cú pháp khác nhau

Như 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 bằng cách nhập đúng

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

>>>

>>> lines = """\
.. He took 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
73

Điều này không bao giờ được sử dụng trong bất kỳ mã nào dành cho mục đích sử dụng thực tế. Tuy nhiên, nó có thể hữu ích cho đêm câu đố Python tiếp theo của bạn

Loại bỏ các quảng cáo

So sánh đơn hàng

Một bộ toán tử kiểm tra khác là toán tử so sánh thứ tự. Có bốn toán tử so sánh thứ tự có thể được phân loại theo hai phẩm chất

  • Hướng đi. Nó nhỏ hơn hay lớn hơn?
  • sự nghiêm khắc. Bình đẳng có được phép hay không?

Vì hai lựa chọn là độc lập, nên bạn có

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
570 toán tử so sánh thứ tự. Tất cả bốn được liệt kê trong bảng này

Nhỏ hơn Lớn hơn Nghiêm ngặ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.
.. """
>>> 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
571
>>> lines = """\
.. He took 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
572 Không nghiêm ngặ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.
.. """
>>> 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
573
>>> lines = """\
.. He took 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
574

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

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

>>>

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

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

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

>>>

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

Một lần nữa, vì không có cách rõ ràng nào để xác định thứ tự, nên Python từ chối so sánh chúng. Điều này tương tự với toán tử 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.
.. """
>>> 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
728]. Mặc dù bạn có thể thêm chuỗi vào chuỗi và số nguyên vào số nguyên, nhưng việc thêm chuỗi vào số nguyên sẽ tạo ra một ngoại lệ

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

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

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

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

>>> lines = """\
.. He took 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
571 không cho phép bình đẳng, trong khi
>>> lines = """\
.. He took 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
573 thì không

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
76

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

Toán 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.
.. """
>>> 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
578

Toán 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.
.. """
>>> 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
578 kiểm tra danh tính đối tượng. Nói cách khác,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
580 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.
.. """
>>> 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
1 khi
>>> lines = """\
.. He took 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
582 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
583 đánh giá cho cùng một đối tượng. Toán 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.
.. """
>>> 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
578 có một toán tử ngược lại, toán 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.
.. """
>>> 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
585

Cách sử dụng thông thường của

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
578 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
585 là để so sánh các danh sách để nhận 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.
.. """
>>> 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
77

Mặc dù

>>> lines = """\
.. He took 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
588, chúng không phải là cùng một đối tượng. Toán 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.
.. """
>>> 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
585 luôn trả về giá trị ngược lại của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
578. Không có sự khác biệt giữa 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.
.. """
>>> 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
591 và 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.
.. """
>>> 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
592 ngoại trừ khả năng đọc

Hãy nhớ rằng các ví dụ trên cho thấy toán 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.
.. """
>>> 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
578 chỉ được sử dụng với danh sách. Hành vi của toán 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.
.. """
>>> 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
578 trên các đối tượng bất biến như số và chuỗi phức tạp hơn

Loại bỏ các quảng cáo

Toán 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.
.. """
>>> 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
595

Toán 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.
.. """
>>> 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
595 kiểm tra tư cách thành viên. Một đối tượng có thể xác định những gì nó coi là thành viên. Hầu hết các trình tự, chẳng hạn như danh sách, coi các phần tử của chúng là thành viê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
78

>>> lines = """\
.. He took 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
718 là một phần tử của danh sách, nê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
598 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
45 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
801 không có trong danh sách, các biểu thức khác trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Trong mọi trường hợp, toán 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.
.. """
>>> 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
595 trả về giá trị Boolean

Vì các chuỗi là các chuỗi ký tự, nên bạn có thể mong đợi chúng cũng kiểm tra tư cách thành viên. Nói cách khác, các ký tự là thành viên của chuỗi sẽ trả về

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 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" 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
595, trong khi những ký tự không phải là thành viên sẽ trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3

>>>

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

>>> lines = """\
.. He took 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
807 là phần tử thứ hai của chuỗi, ví dụ đầu tiên trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
582 không xuất hiện trong chuỗi, ví dụ thứ hai trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Tuy nhiên, cùng với các ký tự riêng lẻ, các chuỗi con cũng được coi là thành viên của một chuỗ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
50

>>> lines = """\
.. He took 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
811 là một chuỗi con, nên toán 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.
.. """
>>> 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
595 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
814 không phải là chuỗi con nên toán 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.
.. """
>>> 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
595 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Điều này bất chấp thực tế là mọi chữ cái riêng lẻ trong
>>> lines = """\
.. He took 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
814 đều là thành viên của chuỗi

Giống như các toán 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.
.. """
>>> 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
578 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
555, toán 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.
.. """
>>> 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
595 cũng có một toán tử ngược lại,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
821. Bạn có thể sử 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.
.. """
>>> 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
821 để 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

Các toán tử so sánh xâu chuỗi

Toán tử so sánh có thể tạo 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 có toán tử so sánh để tạo thành một biểu thức lớn hơn

>>>

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

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.
.. """
>>> 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
823 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ằng 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.
.. """
>>> 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
1 vì cả hai phần của chuỗi đều là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Bạn có thể chia nhỏ chuỗi để xem nó hoạt động như thế nào

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
52

>>> lines = """\
.. He took 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
826 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
828 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1, nê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
767 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Chuỗi so sánh tương đương với việc sử 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.
.. """
>>> 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
767 trên tất cả các liên kết của nó. Trong trường hợp này, vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
833 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1, nên kết quả của cả chuỗi là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Đ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" 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, 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" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
53

Chuỗi so sánh này trả về

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 vì không phải tất cả các liên kết của nó đều là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Bởi vì các chuỗi so sánh là một toán 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.
.. """
>>> 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
767 ẩn, 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" 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, 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" 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. Bạn có thể chia nhỏ chuỗi để xem nó hoạt động như thế nào

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
54

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

  • >>> lines = """\
    .. He took 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
    
    843 là
    >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    1
  • >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> 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
    
    845 là
    >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    3

Đ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.
.. """
>>> 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
1 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" 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. Vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
849 bằng
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 nên giá trị của toàn bộ chuỗi là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3

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

>>>

>>> lines = """\
.. He took 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
55

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

  1. >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> 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
    
    852
  2. >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    853
  3. >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    6

Đâ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 xích sử dụng mộ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.
.. """
>>> 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
767 ngầm, thì xích cũng phải đoản mạch. Điều này rất quan trọng vì ngay cả trong trường hợp so sánh đơn hàng không được xác định, chuỗi vẫn có thể trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3

>>>

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

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
857 ước tính 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" 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 vì nó không đánh giá phép so sánh thứ hai. Trong trường hợp này, việc đánh giá ngắn mạch sẽ ngăn chặn tác dụng phụ khác. nâng cao một ngoại lệ

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

>>>

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

Chia

>>> lines = """\
.. He took 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
45 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" 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
47 sẽ tạo ra mộ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.
.. """
>>> 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
861. Tuy nhiên, do đánh giá ngắn mạch, Python không đánh giá phép chia không hợp lệ. Điều này có nghĩa là Python bỏ qua việc đánh giá không chỉ phép so sánh mà còn cả đầu vào của phép so sánh

Một khía cạnh quan trọng khác cần hiểu về chuỗi so sánh là khi Python đánh giá một phần tử trong chuỗi, nó chỉ đánh giá nó một lần

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
58

Vì các phần 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 khi cấu trúc lại

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
862 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" 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
863. Mặc dù chuỗi hoạt động giống 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" 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
767 trong quá trình đánh giá ngắn mạch, nhưng nó chỉ đánh giá tất cả các giá trị, bao gồm cả các giá trị trung gian, một lần

Chuỗi đặc biệt hữu ích để kiểm tra phạm vi, xác nhận rằng giá trị nằm trong 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

>>>

>>> lines = """\
.. He took 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
59

Nếu có

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47 giờ làm việc, thì không có lý do gì để gửi hóa đơn. Tính đến Giờ tiết kiệm ánh sáng ban ngày, số giờ tối đa trong một ngày là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
866. 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

Toán tử trộn 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

>>> lines = """\
.. He took 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
555,
>>> lines = """\
.. He took 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
556 và so sánh thứ tự. Tuy nhiên, bạn có thể xâu chuỗi tất cả các toán tử so sánh của Python. Điều này có thể dẫn đến hành vi đáng ngạc nhiê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
80

Bởi vì

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
869 là một chuỗi so sánh, nó đánh giá là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1. Bạn có thể chia chuỗi thành các phần của nó

  • 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.
    .. """
    >>> 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
    
    871 là
    >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    1, vì nó sẽ dành cho bất kỳ giá trị nào được đánh giá dựa trên chính nó
  • 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.
    .. """
    >>> 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
    
    873 là
    >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    1 vì
    >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    47 nhỏ hơn
    >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" in line for line in line_list]
    [False, True, True, False]
    >>> False + True + True + False
    2
    >>> len[line_list]
    4
    >>> 2/4
    0.5
    
    45

Vì cả hai phần đều là

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1, nên chuỗi có giá trị là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1

Tuy nhiên, những người đã quen với các toán tử khác trong Python có thể cho rằng, giống như các biểu thức khác bao gồm nhiều toán tử, chẳng hạn 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" 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
879, Python chèn dấu ngoặc đơn vào biểu thức. Tuy nhiên, cả hai cách chèn dấu ngoặc đơn đều không cho kết quả là _________ 01

Bạn có thể thấy tại sao cả hai đánh giá là

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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 nếu bạn chia nhỏ các biểu thức. Nếu bạn chia nhỏ biểu thức đầu tiên, bạn sẽ nhận được như sau

>>>

>>> lines = """\
.. He took 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
81

Bạn có thể thấy ở trên rằng

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
871 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1, giống như đối với bất kỳ giá trị nào. Điều này có nghĩa là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
884 giống 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" 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
885. Booleans là các 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.
.. """
>>> 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
1 bằng 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" 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
45. Vì vậy,
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
885 giống 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" 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
889. Vì đây là bất đẳng thức nghiêm ngặt và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
890, nên nó trả về Sai

Biểu thức thứ hai hoạt động khác đ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
82

>>> lines = """\
.. He took 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
47 nhỏ hơn
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
45, nê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
873 trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1. Vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
895, nên không thể có trường hợp
>>> lines = """\
.. He took 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
896

Ghi chú. Đừng xem 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" 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
897 ở trên. Sử 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.
.. """
>>> 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
578 trên các số có thể gây nhầm lẫn. Tuy nhiên, cụ thể đối với trường hợp bạn biết các số không bằng nhau, bạn có thể biết rằng
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
578 cũng sẽ trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3. Mặc dù ví dụ này đúng, nhưng nó không phải là ví dụ về phong cách viết mã Python tốt

Bài học quan trọng nhất rút ra từ điều này là việc so sánh chuỗi với

>>> lines = """\
.. He took 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
578 thường không phải là một ý tưởng hay. Nó khiến người đọc bối rối và có lẽ không cần thiết

Giống 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" 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
578, toán 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.
.. """
>>> 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
595 và toán tử đối lập của 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
821, thường có thể mang lại kết quả đáng ngạc nhiên khi được xâu chuỗ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
83

Để tối đa hóa sự nhầm lẫn, ví dụ này so sánh chuỗi với các toán tử khác nhau và sử 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.
.. """
>>> 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
595 với các chuỗi để kiểm tra các chuỗi con. Một lần nữa, đây không phải là một ví dụ về mã được viết tốt. Tuy nhiên, điều quan trọng là có thể đọc ví dụ này và hiểu tại sao nó trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1

Cuối cùng, bạn có thể xâu chuỗ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
585 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" 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
821

>>>

>>> lines = """\
.. He took 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
84

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733 trong hai toán tử không giống nhau. Các toán tử phủ định là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
585 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
821. Điều này tương ứng với cách sử dụng thông thường trong tiếng Anh, nhưng rất dễ mắc lỗi khi sửa mã

Loại bỏ các quảng cáo

Kiểm tra Boolean Python

Cách sử dụng phổ biến nhất cho Python Boolean là trong câu lệ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" 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
812. Câu lệnh này sẽ thực thi nếu giá trị là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
85

>>> lines = """\
.. He took 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
814 chỉ được gọi khi biểu thức có giá trị là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
1. Tuy nhiên, trong Python bạn có thể gán 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.
.. """
>>> 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
812. Các giá trị mà
>>> lines = """\
.. He took 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
812 coi là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1 được gọi là trung thực và các giá trị mà
>>> lines = """\
.. He took 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
812 coi là của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 được gọi là giả

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
812 quyết định giá trị nào là trung thực và giá trị nào là giả bằng cách gọi nội 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.
.. """
>>> 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
822 tích hợp. Bạn đã từng gặp
>>> lines = """\
.. He took 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
822 dưới dạng kiểu Boolean của Python. Khi được gọi, nó chuyển đổi các đối tượng thành Booleans

>>> lines = """\
.. He took 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
824 dưới dạng Giá trị Boolean

Đối tượng đơn lẻ

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

>>>

>>> lines = """\
.. He took 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
86

Điều này thường hữu ích trong các câu lệ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" 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
812 kiểm tra giá trị trọng điểm. Tuy nhiên, tốt hơn hết là kiểm tra rõ ràng danh tính 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" 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
827. Đôi khi,
>>> lines = """\
.. He took 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
824 có thể hữu ích khi kết hợp với đánh giá ngắn mạch để có giá trị mặc định

Ví dụ: bạn có thể sử 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.
.. """
>>> 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
503 để thay 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.
.. """
>>> 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
824 bằng một danh sách trống

>>>

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

Trong ví dụ này, danh sách sẽ không được tạo nếu

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
831 là danh sách không trống vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 sẽ đoản mạch trước khi đánh giá
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
833

Số dưới dạng giá trị Boolean

Đối với các số,

>>> lines = """\
.. He took 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
834 tương đương 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" 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
835. Điều này có nghĩa là số nguyên giả duy nhất là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47

>>>

>>> lines = """\
.. He took 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
88

Tất cả các số nguyên khác không đều đúng. Điều này cũng đúng với các số dấu phẩy động, bao gồm các số dấu phẩy động đặc biệt như vô cực và Không phải là số [NaN]

>>>

>>> lines = """\
.. He took 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
89

Vì vô cùng và NaN không bằng

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47, nên chúng là sự thật

So sánh đẳng thức và bất đẳng thức trên các số dấu phẩy động là các phép toán tinh tế. Vì làm

>>> lines = """\
.. He took 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
834 tương đương 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" 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
835, điều này có thể dẫn đến kết quả đáng ngạc nhiên cho các số dấu phẩy động

>>>

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

Tính toán số dấu phẩy động có thể không chính xác. Do đó, kết quả của

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
822 trên các số dấu phẩy động có thể gây ngạc nhiên

Python có nhiều kiểu số hơn trong thư viện chuẩn và chúng tuân theo cùng một quy tắc. Đối với các loại số không tích hợp sẵn,

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
834 cũng tương đương 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" 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
835. Mô-đun
>>> lines = """\
.. He took 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
843 nằm trong thư viện chuẩn. Giống như các loại số khác, phân số giả duy nhất là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
844

>>>

>>> lines = """\
.. He took 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
81

Như với số nguyên và số dấu phẩy động, phân số chỉ sai khi chúng bằng

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

Mô-đun

>>> lines = """\
.. He took 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
846 cũng có trong thư viện chuẩn. Các số thập phân chỉ sai khi chúng bằng _______347

>>>

>>> lines = """\
.. He took 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
82

Số

>>> lines = """\
.. He took 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
848 là một xấp xỉ của Pi đến hai chữ số thập phân. Thực tế này đã được Archimedes thảo luận vào thế kỷ thứ 3 trước Công nguyên. Khi sự khác biệt giữa
>>> lines = """\
.. He took 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
848 và Pi được tính toán với độ chính xác này, kết quả là sai. Khi sự khác biệt được tính toán với độ chính xác cao hơn, sự khác biệt không bằng
>>> lines = """\
.. He took 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
47 và sự thật cũng vậy

Loại bỏ các quảng cáo

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

Nói chung, các đối tượng có ________ 3851 sẽ bị sai khi kết quả của ________ 3851 là

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47. Không thành vấn đề nếu chúng là danh sách, bộ dữ liệu, bộ, chuỗi hoặc chuỗi byte

>>>

>>> lines = """\
.. He took 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
83

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

Các loại khác như giá trị Boolean

Trừ khi các loại có

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
851 hoặc xác định cụ thể liệu chúng là trung thực hay giả mạo, chúng luôn trung thực. Đ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 biệt, các chức năng luôn trung 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.
.. """
>>> 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
84

Phương pháp cũng luôn đúng. Bạn có thể gặp phải trường hợp này nếu thiếu dấu ngoặc đơn khi gọi hàm hoặc phương 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.
.. """
>>> 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
85

Điều này có thể xảy ra do quên dấu ngoặc đơn hoặc tài liệu gây hiểu lầm không đề cập đến việc bạn cần gọi hàm. Nếu bạn mong đợi một giá trị Boolean 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 xác định luôn trung 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.
.. """
>>> 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
86

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

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

>>>

>>> lines = """\
.. He took 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
87

Bạn cũng có thể sử 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.
.. """
>>> 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
855 để tạo một đối tượng không trung thực cũng không giả dố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
88

Câu lệ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" 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
812 cũng sử 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.
.. """
>>> 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
855. Nó làm như vậy để đánh giá xem đối tượng là thật hay giả, điều này sẽ xác định nhánh nào sẽ thực thi

Nếu bạn định nghĩa phương 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.
.. """
>>> 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
859 trên một lớp, thì các thể hiện của nó có mộ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.
.. """
>>> 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
851. Trong trường hợp đó, giá trị Boolean của các phiên bản sẽ sai chính xác khi độ dài của chúng là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47

>>>

>>> lines = """\
.. He took 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
89

Trong ví dụ 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.
.. """
>>> 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
862 sẽ trả về
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47 trước phép gán và sau đó là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
864. Tuy nhiên, điều ngược lại là không đúng. 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.
.. """
>>> 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
855 không cung cấp cho các trường hợp độ dà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
90

Định nghĩa

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
855 không làm cho các thể hiện của một trong hai lớp có mộ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.
.. """
>>> 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
851. Khi cả
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
855 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
869 đều được xác định, 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.
.. """
>>> 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
855 sẽ được ưu tiên

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
91

Mặc dù

>>> lines = """\
.. He took 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
582 có độ dài bằng
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
872 nhưng nó vẫn là giả

Loại bỏ các quảng cáo

Thí dụ. Mảng NumPy

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

Mảng, giống như các con số, là sai hoặc đúng tùy thuộc vào cách chúng so sánh 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" 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
47

>>>

>>> lines = """\
.. He took 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
92

Mặc dù

>>> lines = """\
.. He took 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
582 có độ dài là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
45 nhưng nó vẫn sai vì giá trị 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" 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
47

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
93

Ngoại lệ dài dòng đến mức để dễ đọc, mã sử dụng xử lý văn bản để ngắt dòng

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47. Như bạn đã thấy ở trên, đó không phải là hai câu trả lời khả dĩ duy nhất. Các mảng cũng có thể từ chối có giá trị Boolean

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
94

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

Toán tử và hàm

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

Các toán 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.
.. """
>>> 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
767,
>>> lines = """\
.. He took 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
503 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733 chấp nhận bất kỳ giá trị nào hỗ trợ kiểm tra Boolean. Trong trường hợp của
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733, nó sẽ luôn trả về giá trị Boolean

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
95

Bảng chân lý của

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733 vẫn đúng, nhưng bây giờ nó lấy tính trung thực của đầu vào

Trong trường hợp của

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503, ngoài việc đánh giá ngắn mạch, chúng còn trả về giá trị mà chúng đã dừng đánh giá

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
96

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

Giả sử bạn có một hàm tên là

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
885, nếu văn bản quá dài, nó sẽ lấy phần đầu và phần cuối và thêm dấu chấm lửng [
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
886] ở giữa. Điều này có thể hữu ích trong một số báo cáo không thể hiển thị toàn văn. Tuy nhiên, một số bộ dữ liệu bị thiếu các giá trị được đại diện bở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
824

>>> lines = """\
.. He took 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
885 giả sử đầu vào là một chuỗi, nó sẽ thất bại trê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
824

>>>

>>> lines = """\
.. He took 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
97

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
824 và thực tế là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503 không chỉ đoản mạch mà còn trả về giá trị cuối cùng được đánh giá. Mã để in báo cáo thêm
>>> lines = """\
.. He took 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
892 vào đối số 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" 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
885. Việc bổ sung
>>> lines = """\
.. He took 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
892 giúp bạn tránh lỗi chỉ với một thay đổi mã nhỏ

Các hàm tích hợp

>>> lines = """\
.. He took 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
895 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
896 đánh giá tính trung thực và cả đoản mạch, nhưng chúng không trả về giá trị cuối cùng được đánh giá.
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
895 kiểm tra xem tất cả các đối số của nó có đúng không

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
98

Ở dòng cuối 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.
.. """
>>> 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
895 không đánh giá
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
899 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" 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
45. Vì
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
901 là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47, nên điều này sẽ làm tăng
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
861

>>> lines = """\
.. He took 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
896 kiểm tra xem có bất kỳ đối số nào của nó là trung thực không

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
99

Ở dòng cuối 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.
.. """
>>> 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
896 không đánh giá
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> 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
906 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" 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
47

Loại bỏ các quảng cáo

Sự kết luận

Boolean trong Python là kiểu dữ liệu được sử dụng phổ biến với nhiều ứng dụng hữu ích. Bạn có thể sử dụng Booleans với các toán tử 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" 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
733,
>>> lines = """\
.. He took 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
767,
>>> lines = """\
.. He took 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
503,
>>> lines = """\
.. He took 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
595,
>>> lines = """\
.. He took 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
578,
>>> lines = """\
.. He took 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
555 và
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
556 để so sánh các giá trị và kiểm tra tư cách thành viên, danh tính hoặc đẳng thức. Bạn cũng có thể sử dụng kiểm tra Boolean với câu lệ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" 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
812 để kiểm soát luồng chương trình của mình dựa trên tính trung thực của một biểu thức

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

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

Bây giờ bạn đã biết cách hoạt động của đánh giá ngắn mạch và nhận ra mối liên hệ giữa Booleans và câu lệ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" 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
812. Kiến thức này sẽ giúp bạn hiểu mã hiện có và tránh những cạm bẫy phổ biến có thể dẫn đến lỗi trong chương trình của riêng bạn

Đánh dấu là đã hoàn thành

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Boole Python. Tận dụng các giá trị của sự thật

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Moshe Zadka

Moshe đã sử dụng Python từ năm 1998. Anh ấy đã đóng góp cho CPython và là thành viên sáng lập của dự án Twisted. Anh ấy đã dạy Python ở nhiều địa điểm khác nhau từ năm 2002

» Thông tin thêm về Moshe

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Geir Arne

Jim

Joanna

Gia-cốp

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

__ bool __ trong Python là gì?

__bool__ Chuyển đổi một mảng boolean không chiều thành một đối tượng bool Python . self [array] – thể hiện của mảng không chiều. Phải có kiểu dữ liệu boolean.

__ phương thức __ trong Python là gì?

__enter__ và __exit__ được sử dụng với khối 'with' trong python. __call__ method được sử dụng để sử dụng đối tượng làm phương thức . Phương thức __iter__ được sử dụng để tạo các đối tượng trình tạo bằng cách sử dụng đối tượng.

__ gọi __ Python là gì?

Phương thức __call__ cho phép các lập trình viên Python viết các lớp trong đó các thể hiện hoạt động giống như các hàm và có thể được gọi giống như một hàm . Khi thể hiện được gọi là một hàm; . ] là viết tắt của x. __gọi__[arg1, arg2,. ].

bool ['false'] trả về cái gì?

Trả về giá trị từ bool[] . False if the parameter or value passed is False.

Chủ Đề