Không phải là con trăn sai?

con trăn. Python Boolean. Tối ưu hóa mã với giá trị thực
Key learning qua video
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript

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

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

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
0là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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
2là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 đánh giá Boolean trong Python hoạt động rất quan trọng để thiết lập trình tốt bằng Python

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

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

Kiểu Boolean Python

Boolean Type in Python only have two value can be

  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ó ______06như loại của nó. Bạn có thể kiểm tra loại 

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

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

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

Python Booleans làm Từ khóa

Hiện tại tên 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 chỉ định cho họ, thì bạn sẽ ghi đè giá trị hợp nhất sẵn có

Ngược lại, các 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
1và không phả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 là cái sẵn. We are từ khóa. Không giống như nhiều từ khóa Python khác , và là các biểu thức Python. Vì chúng là biểu thức nên chúng có thể được sử dụng ở bất kỳ nơi nào có thể sử dụng các biểu thức khác nhau, 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
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
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

Because

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ể chỉ định 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à một 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ạo 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
43 khi phân tích cú pháp

Booleans Python dưới dạng số

Booleans được coi là một loại 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 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ố 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
1 giá trị. Đ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 câu trong bài thơ cổ điển dành cho trẻ em để xem có bao nhiêu phần của 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 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 cho phép bạn biết số lần 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1xuất hiện trong trình tạo. Number times 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
1trong bộ tạo bằng 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. This number for the line total number will for you the Rate between the linesphù hợp với tổng số dòng

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

>>>

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

Các 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 biến giữ một danh sách các dòng. Dòng đầu tiên không có tiếng nó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
49trong 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
709là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
49không 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
712là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ì Boolean 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
714cho 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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

Because

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

Toán tử Boolean

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

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

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, có thể chỉ định các toán tử hoàn toàn theo kết quả mà chúng ta chỉ định cho mọi kết hợp đầu vào có thể có. Các thông số kỹ thuật này được gọi là bảng sự thật vì chúng được hiển thị trong một bảng

Như bạn sẽ thấy ở phần sau, trong một số vấn đề, hãy biết đầu vào cho một thuật toán 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á. This is call is đánh giá ngắn mạch

Tầm quan trọng của việc đánh giá việc rút 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ể ảnh hưởng ít đến chương trình của bạn. Trong các trường hợp khác nhau, 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 đáng kể. Trong những trường hợp khắc nghiệt nhất, tính toán đúng đắn của mã của bạn có thể phụ thuộc vào việc đánh giá ngắn mạch

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

Bạn có thể nghĩ về 

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

Việc coi các giá trị Boolean trong Python như các thuật toán đôi khi rất hữu ích. Ví dụ. cách tiếp cận này giúp nhắc nhở bạn rằng chúng không phải là các biến. Vì lý do tương tự mà bạn không thể chỉ định 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ể chỉ định 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ỉ có hai giá trị Boolean trong Python tồn tại. Toán tử Boolean không có đầu vào luôn trả về cùng một giá trị. By because this thing, 

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

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

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

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

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

>>>

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ể tự hỏi tại sao không có toán tử Boolean nào khác nhận một đối số duy nhất. Để hiểu lý thuyết tại sao, bạn có thể nhìn vào bảng hiển thị tất cả các toán tử Boolean về mặt lý thuyết có thể sử dụng một số đố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ính ĐúngKhông
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ó khả năng tính toán khả thi với một đối số. Ngoài ra 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ả lại đầu vào của nó, bạn chỉ có thể xóa nó khỏi mã của mình mà không ảnh hưởng gì

  • >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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à một thuật toán rút ngắn mạch vì nó không phụ thuộc vào các đố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
    
    1và 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 thuật toán rút ngắn mạch khác vì nó không phụ thuộc vào các đố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
    
    3và nhận được kết quả tương tự

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

Các ______1767Boolean điều hành

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
767nhà điều hành phải mất hai đối số. Nó đánh giá 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
3 ngoại trừ khi cả hai đầu vào 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 độ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
767 bằng bảng sự thật 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

This table long line. Tuy nhiên, nó minh họa hành động 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
739là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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
773không quan trọng. By because this thing, 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767ngắn mạch nếu đầ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 thứ 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
3, thì đầu vào thứ hai không được đánh giá

Mã sau có đầu vào thứ hai có, trong ra, để 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 vào. 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 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767toán tử. Nhận biểu thức được hiển thị bởi các đối tượng đo lường mạch là rất quan trọng khi các biểu thức có tác dụng phụ. Trong hai ví dụ cuối cùng, đánh giá việc rút ngắn thời gian tạm dừng tác dụng phụ khi xảy ra

Một ví dụ trong hành động này có thể quan trọng là trong mã có thể tạo 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
794này thừa nhận là bù xù và nhiều linters sẽ cảnh báo về việc làm theo biểu thức 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
795là vô dụng. Mục đích phục vụ không thành cô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
47dưới dạng 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
47không hợp lệ. Tuy nhiên, dòng cuối cùng không nêu ra một ngoại lệ. Đánh giá ngắn mạch, hàm không được gọi, cho phép 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
47cho không xảy ra và không có ngoại lệ nào được nêu 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ẽ nêu ra một ngoại lệ. Trong trường hợp đó, giá trị của đầu vào thứ hai sẽ cần thiết cho kết quả của 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. 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
47và một ngoại lệ sẽ được đưa ra

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

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
503toán 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
1ngoại trừ khi cả hai đầu vào 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
3. 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
503nhà điều hành cũng có thể được xác định bởi bảng sau 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
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

Dòng bảng này dài, nhưng nó có ý nghĩa tương tự như phần 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à hoặc được sử dụng trong cụm từ "Bạn có thể cách tập đơn gia hạn hoặc cuối bài về nhà đúng hạn. " Trong trường hợp này, bạn không thể sao chép đơn xin gia hạn, sao chép tập tin về nhà đúng hạn

  • Bao gồm hoặc gấp đôi khi được định nghĩa chỉ bằng cách sử dụng kết hợp và / hoặc. Ví dụ. “Nếu bạn làm tốt nhiệm vụ này, thì bạn có thể được tăng lương và / hoặc thăng chức” có nghĩa là bạn có thể được tăng lương và 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 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
503là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 bao gồm 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
503toán tử trong Python cũng sử dụng đánh giá rút 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 chứng minh đá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

Dữ liệu đầ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 thứ 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
3. Trên thực tế, đánh giá đoả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 được sử dụng ít hơn nhiều so với đánh giá 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ả cá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

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

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 xa lạ khi cần thiết trong thực tế. By because this, 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, and 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
503chỉ được xây dựng trong các nhà khai thác Python Boolean

So sánh toán tử

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

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

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

Các biến so sánh toán tử phổ biến nhất là toán tử bì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
555] và toán tử bất bì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
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ử bì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
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ạm quan sát để xem dữ liệu một số điều kiện đã được phát hiện hay chưa. Đôi khi bạn cần so sánh kết quả từ hai hàm với nhau

Toán tử bình đẳng được sử 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

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

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

Lưu ý. Ngôn ngữ Python không thực thi điề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
555và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
556trả về Booleans. Các thư viện như NumPy và pandas trả về các giá trị khác

Chỉ đứng sau toán tử bình đẳng biến là toán tử không bì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
556]. Nó được trả lại 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 có. Các ví dụ tương tự nhau trên phạm vi rộng. Nhiều bài kiểm tra đơn vị kiểm tra rằng giá trị không bằng một giá trị không hợp lệ cụ thể. Ứng dụng web khách có thể kiểm tra xem có phải mã lỗi 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
565 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 đang đượ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ó thể điều đáng ngạc nhiên nhất về toán tử bất đẳng thức Python là thực tế là nó tồn tại ngay từ đầu. Cuối cùng, 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
566vớ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ác cú pháp bổ sung, và đặc biệt là các toán tử lõi phụ, vì những thứ có thể dễ dàng đạt được bằng các phương tiện tiện lợi khác

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
568loạt bài này, thực sự có hai cú pháp khác nhau

Như một trò đùa của ngày Cá tháng Tư, Python vẫn hỗ trợ một cú pháp thay thế cho sự bất bình đẳng với công việ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
569nhậ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
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 đối với Python tiếp theo của bạn

So sánh đơn hàng

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

  • hướng. Nó nhỏ hơn hay lớn hơn?
  • Tính năng ràng buộc. Có được phép bình đẳng hay không?

Vì hai lựa chọn là độc lập, bạn nhận được 

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

Ít hơnLớn hơnNghiêm 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
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
572Không nghiêm 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
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ề hướng đi và hai lựa chọn về sự nghiêm trọng. Điều này dẫn đến tổng số bốn toán tử so sánh đơn hàng

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

>>>

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

Unknown clear dictionaryẽ được sắp xếp như thế nào. Theo Zen of Python , khi đối mặt với sự thật về giấc mơ, Python từ chối đoán

Mặc dù chuỗi và được sắp xếp riêng biệt, so sánh giữa các loại không được hỗ 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
75

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

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, việc thêm chuỗi vào số nguyên sẽ dẫn đến một ngoại lệ

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

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

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

So sánh các số trong Python là một biến phổ biến để kiểm tra các điều kiện biên. Lưu ý rằng điề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
571khô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điề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
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 họ trả về một giá trị Python Boolean

Ngườ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 điều hành

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

Một cách sử dụng điển hình 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
578và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
585là để so sánh danh sách cho danh 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
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ề số đố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 nào 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 không thay đổi như số và chuỗi là

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

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
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 nên 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ề một 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

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
807 là phần tử thứ hai của chuỗi nên 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 nên 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

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
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à 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
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 phép toán 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 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

Kể từ 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
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, 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Một 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 đượ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
3vì không phải tất cả các liên kết của nó đều như vậy 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767toán tử ngầm , nếu ngay cả một liên kết là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
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 Boolean after

  • >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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
1và 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, 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 phí 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 thiết yếu nhất phải giống nhau. Ngoài chí không phải tất cả các loại đều giống nhau. Trong các ví dụ trên, bạn có ba kiểu số

  1. >>> lines = """\
    .. He took his vorpal sword in hand;
    ..       Long time the manxome foe he sought—
    .. So rested he by the Tumtum tree
    ..       And stood awhile in thought.
    .. """
    >>> line_list = lines.splitlines[]
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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 kiểu số khác nhau, nhưng bạn có thể so sánh các đối tượng thuộc các kiểu số khác nhau mà không gặp vấn đề gì

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

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

Đánh giá rút 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 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
45theo 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
47lẽ ra sẽ nâng cao 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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á sự phân chia không hợp lệ. Điều này có nghĩa là Python bỏ qua việc đánh giá không chỉ so sánh mà còn cả các đầu vào cho phép so sánh

Một khía cạnh khác rất quan trọng để hiểu về chuỗi so sánh là khi Python thực hiện đánh giá một yếu tố thành phần trong chuỗi, nó đánh giá nó một lần duy nhấ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
58

Bởi vì các yếu tố trung tâm chỉ được đánh giá một lần, nó không phải lúc nào cũng an toàn để 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 để 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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
767trong đánh giá ngắn mạch, nhưng nó đánh giá tất cả các giá trị, bao gồm cả các giá trị trung gian, chỉ một lần

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 đều 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ên 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, giống như 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 có 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ể hiểu tại sao cả hai đều đá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. Boolean là kiểu 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 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ậ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

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

Ghi chú. Đừng coi thườ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
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ể. 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 theo 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 ý kiến ​​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ử 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, 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

Để giảm thiểu sự nhầm lẫn, ví dụ này ghép chuỗi các chuỗi được phép so sánh 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
595vớ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ó được trả lại
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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
585vớ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
733hai toán tử không giống nhau. Các phủ định về thanh toá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
585và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ễ gặp lỗi khi sửa đổi mã

Thử nghiệm Boolean trong Python

Cách sử dụng biến phổ biến nhất cho Python Boolean là trong 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
812câu lệnh. Câu lệnh này sẽ thực thi nếu giá trị là
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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
814chỉ được gọi khi biểu thức đánh giá là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ể đưa ra bất kỳ giá trị nào ______3812. 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
812coi 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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à  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
812coi 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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à

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
812quyết định giá trị nào là trung thực và giá trị nào là sai bằng cách gọi nội bộ
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. You have stopped phả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
822kiểu Boolean trong 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 định 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
824luôn 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
86

Điều này thường hữu ích trong 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
812câu lệnh kiểm tra một giá trị trọng điểm. Tuy nhiên, tốt hơn là bạn nên 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
824có thể hữu ích khi kết hợp với đánh giá rút 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
831là 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ẽ rút ngắn mạch trước khi nó đánh giá 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
833

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

Argument with number, 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
834tươ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 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 other integers đều không phải là trung thực. Điều này cũng đúng với  , bao gồm các số dấu phẩy động đặc biệt như  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
89

Vì vô cực và NaN không bằng nhau 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
47 họ là thật

So sánh bình đẳng và bất bình đẳng 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
834tươ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 kinh ngạc cho các dấu hiệu hài hướ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
80

Các tính năng cho phép của dấu phẩy động có thể không chính xác. Do đó, kết quả của 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
822dấu hiệu 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ác quy tắc tương tự. Đối với các kiểu số không cài đặ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
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. 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
843mô-đun nằm trong thư viện chuẩn. Giống như các kiểu số khác nhau, sai số 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 nguyên số và 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

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
846mô-đun cũng nằm trong thư viện chuẩn. Các số phân tích chỉ giả mạo tương tự 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

>>>

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

Con 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
848là 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 có 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
848và Pi được tính với độ chính xác này, kết quả là sai. Khi sự khác biệt được tính toán với mức độ 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

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

Nói chung, các đối tượng 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ý muốn là sai khi 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
851là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 quan trọng nếu chúng là danh sách, bộ giá trị, 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 build đều có độ dài dài đều hành theo quy tắc này. Sau đó, bạn sẽ thấy một số ngoại lệ đối với quy tắc này đối với các đối tượng không được tạo sẵn

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

Ngoại trừ khi các loại 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 hoặc cụ thể xác định có thể xem chúng là trung thực hay giả dối, chúng luôn luôn trung thực. Điều này đúng với các kiểu cài đặt sẵn có 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

Các phương pháp cũng luôn trung thực. Bạn có thể gặp phải điều này nếu thiếu dấu trích lệnh khi bạn gọi một 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 dấu trích đơn bị quên hoặc tài liệu gây hiểu lầm không đề cập đến việc bạn cần gọi hàm. Nếu bạn mong đợi một giá trị Boolean trong Python nhưng có một hàm trả về giá trị Boolean, thì nó sẽ luôn là trung thực

Theo mặc định, các loại người dùng được 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 đó là trung thực. Tất cả các đối tượng đều là trung thực trừ khi các phương thức đặc biệt đã được xác định. Nếu bạn muốn tạo một số trường hợp giả mạo lớp của mình, bạn có thể xác định 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 để làm cho 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 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
812lệnh 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ả, từ đó xác định các chi nhánh sẽ thực hiện điều đó

Nếu bạn đị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
859 phương thức trên một lớp, thì các khả năng của nó có thể 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
851. Trong trường hợp đó, giá trị Boolean của các thể hiện sẽ sai chính xác khi độ dài của chúng là _______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
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 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
864sau đó. Tuy nhiên, điều ngược lại là không đúng. Xác 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
855không cung cấp độ dài cho cá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
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
855không làm cho các cá thể 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ả 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
855và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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được 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ư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
582có chiều 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
872, nó vẫn sai

Ví dụ. Mang 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 Chứng thực chứng minh các trường hợp trong Python. Tuy nhiên, có thể nhận được kết quả tương tự bằng cách sử dụng một trong các thư viện biến phổ biến nhất trên PyPI. NumPy

Mảng , as number, is sai hoặc trung thực 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
582có độ 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ể là sai và một số có thể là trung thực. Trong những trường hợp đó, NumPy sẽ đưa ra một ngoại lệ

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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ệ là dòng quá dài nên để dễ đọc, mã sử dụng xử lý văn bản để bao bọc các dòng

Một trường hợp gần thú vị hơn liên quan đến các khoảng trống. Bạn có thể tự hỏi những tài liệu thứ đó là giả mạo như các trình tự khác hay là sự thật bởi 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ả thi duy nhất. Các mảng cũng có thể từ chối có giá trị Boolean

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

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 là sai , nhưng việc dựa vào hành vi này là rất nguy hiểm. Trong một số phiên bản NumPy trong tương lai, điều này sẽ đưa ra một ngoại lệ

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

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

Các nhà khai 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
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
503và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
733chấp nhận bất kỳ giá trị nào mà hỗ trợ thử nghiệm 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 sự thật 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
733 vẫn đúng, nhưng bây giờ nó có tính xác thực của đầu vào

Trong trường hợp 

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len[line_list]
4
>>> 2/4
0.5
767và 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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á rút ngắn mạch, họ cũng trả về giá trị mà họ đã không ngừng đánh giá

>>>

>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 sự thật vẫn đúng, nhưng giờ đây chúng xác định độ tin cậy của các kết quả, điều này phụ thuộc vào độ tin cậy của các đầu vào. Điều này có thể hữu ích khi bạn muốn đặt giá trị mặc định

Giả sử bạn có một hàm được gọ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
885, nếu văn bản quá dài, hãy 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ể phù hợp với toàn văn. Tuy nhiên, một số tập tin bị thiếu 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 sử dụng đầu vào là một chuỗi nên nó sẽ không thành công 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 sự 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
824và 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
503không chỉ đoản mạch mà còn trả về giá trị cuối cùng được đánh giá cao. Mã để báo cáo 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
892vào đố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
885. Phần bổ sung ________ 3892 này giúp bạn tránh được các lỗi chỉ với một mã thay đổi nhỏ

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

>>>

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

Trong 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
895khô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
899cho 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Kể từ 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
901là 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, điều này sẽ huy động đượ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
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 bất kỳ đối số nào của nó là trung thực hay không

>>>

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

Trong 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
896khô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
906cho 
>>> lines = """\
.. He took his vorpal sword in hand;
..       Long time the manxome foe he sought—
.. So rested he by the Tumtum tree
..       And stood awhile in thought.
.. """
>>> line_list = lines.splitlines[]
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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

Phần kết luận

Python Boolean là một loại 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 hoạt động yêu thí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
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 giá trị và kiểm tra các thành viên, danh tính hoặc bình đẳng. Bạn cũng có thể sử dụng Boolean để kiểm tra 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 tra luồng chương trình của bạn dựa trên tính trung thực của một biểu thức

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

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

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

Chủ Đề