Which of the following is not true about methods

The Python Boolean type is one of Python’s built-in data types. It’s used to represent the truth value of an expression. For example, the expression

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
0 is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, while the expression
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
2 is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Understanding how Python Boolean values behave is important to programming well in Python.

In this tutorial, you’ll learn how to:

  • Manipulate Boolean values with Boolean operators
  • Convert Booleans to other types
  • Convert other types to Python Booleans
  • Use Python Booleans to write efficient and readable Python code

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

The Python Boolean Type

The Python Boolean type has only two possible values:

  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

No other value will have

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 as its type. You can check the type of
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
3 with the built-in
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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:

>>>

>>> type(False)

>>> type(True)

The

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

The type

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 is built in, meaning it’s always available in Python and doesn’t need to be imported. However, the name itself isn’t a keyword in the language. While the following is considered bad style, it’s possible to assign to the name
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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:

>>>

>>> bool

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

Although technically possible, to avoid confusion it’s highly recommended that you don’t assign a different value to

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

Remove ads

Python Booleans as Keywords

Built-in names aren’t keywords. As far as the Python language is concerned, they’re regular variables. If you assign to them, then you’ll override the built-in value.

In contrast, the names

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
3 are not built-ins. They’re keywords. Unlike many other Python keywords,
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
3 are Python expressions. Since they’re expressions, they can be used wherever other expressions, like
>>> def print_and_return(x):
...     print(f"I am returning {x}")
...     return x
...
>>> True and print_and_return(True)
I am returning True
True
>>> True and print_and_return(False)
I am returning False
False
>>> False and print_and_return(True)
False
>>> False and print_and_return(False)
False
1, can be used.

It’s possible to assign a Boolean value to variables, but it’s not possible to assign a value to

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

>>>

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

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
1 is a keyword, you can’t assign a value to it. The same rule applies to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3:

>>>

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

You can’t assign to

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3 because it’s a keyword in Python. In this way,
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
3 behave like other numeric constants. For example, you can pass
>>> def print_and_return(x):
...     print(f"I am returning {x}")
...     return x
...
>>> True and print_and_return(True)
I am returning True
True
>>> True and print_and_return(False)
I am returning False
False
>>> False and print_and_return(True)
False
>>> False and print_and_return(False)
False
8 to functions or assign it to variables. However, it’s impossible to assign a value to
>>> def print_and_return(x):
...     print(f"I am returning {x}")
...     return x
...
>>> True and print_and_return(True)
I am returning True
True
>>> True and print_and_return(False)
I am returning False
False
>>> False and print_and_return(True)
False
>>> False and print_and_return(False)
False
8. The statement
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
0 is not valid Python. Both
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
0 and
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
2 are invalid Python code and will raise a
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
3 when parsed.

Python Booleans as Numbers

Booleans are considered a numeric type in Python. This means they’re numbers for all intents and purposes. In other words, you can apply arithmetic operations to Booleans, and you can also compare them to numbers:

>>>

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

There aren’t many uses for the numerical nature of Boolean values, but there’s one technique you may find helpful. 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
1 is equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5 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
3 is equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7, adding Booleans together is a quick way to count the number of
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 values. This can come in handy when you need to count the number of items that satisfy a condition.

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

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
9, then the fact that
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 is equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5 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
3 is equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 can come in quite handy:

>>>

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

Summing all values in a generator expression like this lets you know how many 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
1 appears in the generator. The number of 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
1 is in the generator is equal to the number of lines that contain the word
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
9, in a case-insensitive way. Dividing this number by the total number of lines gives you the ratio of matching lines to total lines.

To see why this works, you can break the above code into smaller parts:

>>>

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

The

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
07 variable holds a list of lines. The first line doesn’t have the word
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
9 in it, so
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
09 is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. In the second line,
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
9 does appear, so
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
12 is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Since Booleans are numbers, you can add them to numbers, and
>>> bool

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

Since

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
16 is a list of four Booleans, you can add them together. When you add
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
17, you get
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
18. Now, if you divide that result by
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
19, the length of the list, you get
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
20. The word
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
9 appears in half the lines in the selection. This is a useful way to take advantage of the fact that Booleans are numbers.

Boolean Operators

Boolean operators are those that take Boolean inputs and return Boolean results.

Note: Later, you’ll see that these operators can be given other inputs and don’t always return Boolean results. For now, all examples will use Boolean inputs and results. You’ll see how this generalizes to other values in the section on truthiness.

Since Python Boolean values have only two possible options,

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 or
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, it’s possible to specify the operators completely in terms of the results they assign to every possible input combination. These specifications are called truth tables since they’re displayed in a table.

As you’ll see later, in some situations, knowing one input to an operator is enough to determine its value. In those cases, the other input is not evaluated. This is called short-circuit evaluation.

The importance of short-circuit evaluation depends on the specific case. In some cases, it might have little effect on your program. In other cases, such as when it would be computationally intensive to evaluate expressions that don’t affect the result, it provides a significant performance benefit. In the most extreme cases, the correctness of your code can hinge on the short-circuit evaluation.

Remove ads

Operators With No Inputs

You can think of

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
3 as Boolean operators that take no inputs. One of these operators always returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, and the other always returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

Thinking of the Python Boolean values as operators is sometimes useful. For example, this approach helps to remind you that they’re not variables. For the same reason you can’t assign to

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
28, it’s impossible to assign to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
1 or
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

Only two Python Boolean values exist. A Boolean operator with no inputs always returns the same value. Because of 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 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
3 are the only two Boolean operators that don’t take inputs.

The >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 33 Boolean Operator

The only Boolean operator with one argument is

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33. It takes one argument and returns the opposite result:
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 for
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 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
1 for
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Here it is in a truth table:

>>> bool

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

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

This table illustrates that

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 returns the opposite truth value of the argument. Since
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 takes only one argument, it doesn’t short-circuit. It evaluates its argument before returning its result:

>>>

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

The last line shows that

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 evaluates its input before returning
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

You might be wondering why there are no other Boolean operators that take a single argument. In order to understand why, you can look at a table that shows all theoretically possible Boolean operators that would take one argument:

>>> bool

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

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

There are only four possible operators with one argument. Other than

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33, the remaining three operators all have somewhat whimsical names since they don’t actually exist:

  • >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    62: Since this operator simply returns its input, you could just delete it from your code with no effect.

  • >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    63: This is a short-circuit operator since it doesn’t depend on its argument. You could just replace it with
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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 and get the same result.

  • >>> bool
    
    >>> bool = "this is not a type"
    >>> bool
    'this is not a type'
    
    65: This is another short-circuit operator since it doesn’t depend on its argument. You could just replace it with
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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 and get the same result.

None of the other possible operators with one argument would be useful.

The >>> bool >>> bool = "this is not a type" >>> bool 'this is not a type' 67 Boolean Operator

The

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 operator takes two arguments. It evaluates to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 unless both inputs are
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. You could define the behavior of
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 with the following truth table:

>>> bool

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

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
74
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 is verbose. However, it illustrates the same behavior as the description above. If

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
39 is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, then the value of
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
73 doesn’t matter. Because of this,
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 short-circuits if the first input is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. In other words, if the first input is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, then the second input isn’t evaluated.

The following code has a second input that has a side effect, printing, in order to provide a concrete example:

>>>

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

In the last two cases, nothing is printed. The function isn’t called since calling it isn’t necessary to determine the value of the

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 operator. Being aware of short-circuits is important when expressions have a side effect. In the last two examples, the short-circuit evaluation prevents the printing side effect from happening.

One example in which this behavior can be crucial is in code that might raise exceptions:

>>>

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

The function

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
94 is admittedly silly, and many linters would warn about the expression
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
95 being useless. It does serve the purpose of neatly failing when given
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 as a parameter since division by
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 is invalid. However, the last line doesn’t raise an exception. Because of short-circuit evaluation, the function isn’t called, the division by
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 doesn’t happen, and no exception is raised.

In contrast,

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
99 would raise an exception. In that case, the value of the second input would be needed for the result of
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67. Once the second input was evaluated,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
01 would be called, it would divide by
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7, and an exception would be raised.

Remove ads

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

The value of the

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 operator is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 unless both of its inputs are
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. The
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 operator could also be defined by the following truth table:

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
73
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
10
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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

This table is verbose, but it has the same meaning as the explanation above.

When used informally, the word or can have one of two meanings:

  • The exclusive or is how or is used in the phrase “You can file for an extension or submit your homework on time.” In this case, you can’t both file for an extension and submit your homework on time.

  • The inclusive or is sometimes indicated by using the conjunction and/or. For example, “If you do well on this task, then you can get a raise and/or a promotion” means that you might get both a raise and a promotion.

When Python interprets the keyword

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03, it does so using the inclusive or. If both inputs are
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, then the result of
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

Because it uses an inclusive or, the

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 operator in Python also uses short-circuit evaluation. If the first argument is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, then the result is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, and there is no need to evaluate the second argument. The following examples demonstrate the short-circuit evaluation of
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03:

>>>

>>> bool

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

The second input isn’t evaluated by

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 unless the first one is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. In practice, the short-circuit evaluation of
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 is used much less often than that of
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67. However, it’s important to keep this behavior in mind when reading code.

Other Boolean Operators

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

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67, and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 are needed. All other operators on two inputs can be specified in terms of these three operators. All operators on three or more inputs can be specified in terms of operators of two inputs.

In fact, even having both

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 is redundant. The
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 operator can be defined in terms of
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03, and the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 operator can be defined in terms of
>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67. However,
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 are so useful that all programming languages have both.

There are sixteen possible two-input Boolean operators. Except for

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03, they are rarely needed in practice. Because of 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,
>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67, and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 are the only built-in Python Boolean operators.

Comparison Operators

Some of Python’s operators check whether a relationship holds between two objects. Since the relationship either holds or doesn’t hold, these operators, called comparison operators, always return Boolean values.

Comparison operators are the most common source of Boolean values.

Equality and Inequality

The most common comparison operators are the equality operator (

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
55) and the inequality operator (
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56). It’s almost impossible to write any meaningful amount of Python code without using at least one of those operators.

The equality operator (

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
55) is one of the most used operators in Python code. You often need to compare either an unknown result with a known result or two unknown results against each other. Some functions return values that need to be compared against a sentinel to see if some edge condition has been detected. Sometimes you need to compare the results from two functions against each other.

The equality operator is often used to compare numbers:

>>>

>>> bool

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

You may have used equality operators before. They’re some of the most common operators in Python. For all built-in Python objects, and for most third-party classes, they return a Boolean value:

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

Note: The Python language doesn’t enforce that

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
55 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56 return Booleans. Libraries like NumPy and pandas return other values.

Second only to the equality operator in popularity is the inequality operator (

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56). It returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 if the arguments aren’t equal and
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 if they are. The examples are similarly wide-ranging. Many unit tests check that the value isn’t equal to a specific invalid value. A web client might check that the error code isn’t
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
65 before trying an alternative.

Here are two examples of the Python inequality operator in use:

>>>

>>> bool

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

Perhaps the most surprising thing about the Python inequality operator is the fact that it exists in the first place. After all, you could achieve the same result as

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
66 with
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
67. Python usually avoids extra syntax, and especially extra core operators, for things easily achievable by other means.

However, inequality is used so often that it was deemed worthwhile to have a dedicated operator for it. In old versions of Python, in the

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
68 series, there were actually two different syntaxes.

As an April Fools’ joke, Python still supports an alternative syntax for inequality with the right

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

>>>

>>> bool

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

This should never be used in any code meant for real use. It could come in handy for your next Python trivia night, however.

Remove ads

Order Comparisons

Another set of test operators are the order comparison operators. There are four order comparison operators that can be categorized by two qualities:

  • Direction: Is it less than or greater than?
  • Strictness: Is equality allowed or not?

Since the two choices are independent, you get

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
70 order comparison operators. All four are listed in this table:

Less thanGreater thanStrict

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
71
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
72Not strict
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
73
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
74

There are two options for direction and two options for strictness. This results in total of four order comparison operators.

The order comparison operators aren’t defined for all objects. Some objects don’t have a meaningful order. Even though lists and tuples are ordered lexicographically, dictionaries don’t have a meaningful order:

>>>

>>> bool

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

It’s not obvious how dictionaries should be ordered. As per the Zen of Python, in the face of ambiguity, Python refuses to guess.

While strings and integers are ordered separately, intertype comparisons aren’t supported:

>>>

>>> bool

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

Again, since there’s no obvious way to define order, Python refuses to compare them. This is similar to the addition operator (

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
28). Though you can add strings to strings and integers to integers, adding strings to integers raises an exception.

When the order comparison operators are defined, in general they return a Boolean.

Note: Python doesn’t enforce that comparison operators return Booleans. While all built-in Python objects, and most third-party objects, return Booleans when compared, there are exceptions.

For example, comparison operators between NumPy arrays or pandas DataFrames return arrays and DataFrames. You’ll see more about the interaction of NumPy and Boolean values later in this tutorial.

Comparing numbers in Python is a common way of checking against boundary conditions. Note that

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
71 doesn’t allow equality, while
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
73 does:

>>>

>>> bool

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

Programmers often use comparison operators without realizing that they return a Python Boolean value.

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

The

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78 operator checks for object identity. In other words,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
80 evaluates to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 only when
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
82 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
83 evaluate to the same object. The
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78 operator has an opposite, the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
85 operator.

A typical usage of

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
85 is to compare lists for identity:

>>>

>>> bool

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

Even though

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
88, they are not the same object. The
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
85 operator always returns the opposite of
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78. There’s no difference between the expression
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
91 and the expression
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
92 except for readability.

Keep in mind that the above examples show the

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78 operator used only with lists. The behavior of the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78 operator on immutable objects like numbers and strings is more complicated.

Remove ads

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

The

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95 operator checks for membership. An object can define what it considers members. Most sequences, such as lists, consider their elements to be members:

>>>

>>> bool

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

Since

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
18 is an element of the list,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
98 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Since
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5 and
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
01 aren’t in the list, the other expressions return
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3. In all cases, the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95 operator returns a Boolean value.

Since strings are sequences of characters, you might expect them to also check for membership. In other words, characters that are members of the string will return

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

>>>

>>> bool

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

Since

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
07 is the second element of the string, the first example returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Since
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
82 doesn’t appear in the string, the second example returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. However, along with individual characters, substrings are also considered to be members of a string:

>>>

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

Since

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
11 is a substring, the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95 operator returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Since
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
14 is not a substring, the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95 operator returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 despite the fact that every individual letter in
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
14 is a member of the string.

Like the operators

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
55, the
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95 operator also has an opposite,
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
21. You can use
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
21 to confirm that an element is not a member of an object.

Chaining Comparison Operators

Comparison operators can form chains. You can create comparison operator chains by separating expressions with comparison operators to form a larger expression:

>>>

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

The expression

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
23 is a comparison operator chain. It has expressions separated by comparison operators. The result is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 because both parts of the chain are
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. You can break up the chain to see how it works:

>>>

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

Since

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
26 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 and
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
28 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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,
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. A comparison chain is equivalent to using
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 on all its links. In this case, since
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
33 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, the result of the whole chain is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. This means that if any of the links are
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, then the whole chain is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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:

>>>

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

This comparison chain returns

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 since not all of its links are
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Because comparison chains are an implicit
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 operator, if even one link is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, then the whole chain is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. You can break up the chain to see how it works:

>>>

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

In this case, the parts of the chain evaluate to the following Booleans:

  • >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    43 is
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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
  • >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    45 is
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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 means that one of the results is

>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 and one is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Since
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
49 is equal to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3, the value of the entire chain is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

You can mix types and operations in a comparison chain as long as the types can be compared:

>>>

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

The operators don’t have to be all the same. Not even the types have to be all the same. In the examples above, you have three numeric types:

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

These are three different numeric types, but you can compare objects of different numeric types without issue.

Short-Circuit Chain Evaluation

If chains use an implicit

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67, then chains must also short-circuit. This is important because even in cases where an order comparison isn’t defined, it’s possible for a chain to return
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" in line for line in line_list]
[False, True, True, False]
>>> False + True + True + False
2
>>> len(line_list)
4
>>> 2/4
0.5
3:

>>>

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

Even though Python can’t order-compare integers and strings numbers,

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
57 evaluates to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 because it doesn’t evaluate the second comparison. In this case, the short-circuit evaluation prevents another side effect: raising an exception.

Short-circuit evaluation of comparison chains can prevent other exceptions:

>>>

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

Dividing

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5 by
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 would have raised a
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
61. However, because of the short-circuit evaluation, Python doesn’t evaluate the invalid division. This means that Python skips evaluating not only the comparison but also the inputs to the comparison.

Another aspect that is important to understand about comparison chains is that when Python does evaluate an element in the chain, it evaluates it only once:

>>>

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

Because the middle elements are evaluated only once, it’s not always safe to refactor

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
62 to
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
63. Although the chain behaves like
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 in its short-circuit evaluation, it evaluates all values, including the intermediate ones, only once.

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

>>>

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

If there are

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

Mixing Operators and Chaining

Until now, all our examples involved

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

>>>

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

Because

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
69 is a comparison chain, it evaluates to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. You can break the chain into its parts:

  • The expression
    >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    71 is
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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, as it would be for any value evaluated against itself.
  • The expression
    >>> False = 5
      File "", line 1
    SyntaxError: cannot assign to False
    
    73 is
    >>> lines = """\
    ... He took his vorpal sword in hand;
    ...       Long time the manxome foe he sought—
    ... So rested he by the Tumtum tree
    ...       And stood awhile in thought.
    ... """
    >>> line_list = lines.splitlines()
    >>> "the" in line_list[0]
    False
    >>> "the" in line_list[1]
    True
    >>> 0 + False + True # Equivalent to 0 + 0 + 1
    1
    >>> ["the" 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 since
    >>> def inverse_and_true(n):
    ...     1 // n
    ...     return True
    ...
    >>> inverse_and_true(5)
    True
    >>> inverse_and_true(0)
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in inverse_and_true
    ZeroDivisionError: integer division or modulo by zero
    >>> False and inverse_and_true(0)
    False
    
    7 is less than
    >>> def inverse_and_true(n):
    ...     1 // n
    ...     return True
    ...
    >>> inverse_and_true(5)
    True
    >>> inverse_and_true(0)
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in inverse_and_true
    ZeroDivisionError: integer division or modulo by zero
    >>> False and inverse_and_true(0)
    False
    
    5.

Since both parts are

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

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

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
79, Python inserts parentheses into to the expression. However, neither way of inserting parenthesis will evaluate to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

You can see why both evaluate to

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

>>>

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

You can see above that

>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
71 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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, as it would for any value. This means that
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
84 is the same as
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
85. Booleans are numeric types, and
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 is equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5. So
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
85 is the same as
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
89. Since this is a strict inequality, and
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
90, it returns False.

The second expression works differently:

>>>

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

Since

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 is less than
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5,
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
73 returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. Since
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
95, then it can’t be the case that
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
96.

Note: Don’t take the above

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

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

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

Like

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

>>>

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

To maximize the confusion, this example chains comparisons with different operators and uses

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95 with strings to check for substrings. Again, this is not an example of well-written code! However, it’s important to be able to read this example and understand why it returns
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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.

Finally, you can chain

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
85 with
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
21:

>>>

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

Note that the order of

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 in the two operators isn’t the same! The negative operators are
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
85 and
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
21. This corresponds with the regular usage in English, but it’s easy to make a mistake when modifying code.

Remove ads

Python Boolean Testing

The most popular use for a Python Boolean is in an

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 statement. This statement will execute if the value is
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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:

>>>

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

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
14 is called only when the expression evaluates to
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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. However, in Python you can give any value to
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12. The values that
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 considers
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 are called truthy, and the values that
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 considers
>>> lines = """\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """
>>> line_list = lines.splitlines()
>>> "the" in line_list[0]
False
>>> "the" in line_list[1]
True
>>> 0 + False + True # Equivalent to 0 + 0 + 1
1
>>> ["the" 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 are called falsy.

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 decides which values are truthy and which are falsy by internally calling the built-in
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
22. You’ve already encountered
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
22 as the Python Boolean type. When called, it converts objects to Booleans.

>>> True == 1 True >>> False == 0 True >>> True + (False / True) 1.0 24 as a Boolean Value

The singleton object

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
24 is always falsy:

>>>

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

This is often useful in

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 statements that check for a sentinel value. However, it’s usually better to explicitly check for identity with
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
27. Sometimes
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
24 can be useful in combination with short-circuit evaluation in order to have a default.

For example, you can use

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 to substitute
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
24 with an empty list:

>>>

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

In this example, the list won’t be created if

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
31 is a non-empty list since
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 will short-circuit before it evaluates
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
33.

Numbers as Boolean Values

For numbers,

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
34 is equivalent to
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
35. This means the only falsy integer is
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7:

>>>

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

All nonzero integers are truthy. This is also true for floating-point numbers, including special floating-point numbers like infinity and Not a Number (NaN):

>>>

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

Since infinity and NaN aren’t equal to

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

Equality and inequality comparisons on floating-point numbers are subtle operations. Since doing

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
34 is equivalent to
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
35, this can lead to surprising results for floating-point numbers:

>>>

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

Floating-point number computations can be inexact. Because of that, the results of

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
22 on floating-point numbers can be surprising.

Python has more numeric types in the standard library, and they follow the same rules. For non-built-in numeric types,

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
34 is also equivalent to
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
35. The
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
43 module is in the standard library. Like other numeric types, the only falsy fraction is
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
44:

>>>

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

As with integers and floating-point numbers, fractions are false only when they’re equal to

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

The

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
46 module is also in the standard library. Decimals are similarly falsy only when they’re equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7:

>>>

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

The number

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
48 is an approximation of Pi to two decimal places. This fact was discussed by Archimedes in the 3rd century BCE. When the difference between
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
48 and Pi is computed with this precision, the result is falsy. When the difference is computed with higher precision, the difference isn’t equal to
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7, and so is truthy.

Remove ads

Sequences as Boolean Values

In general, objects that have a

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
51 will be falsy when the result of
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
51 is
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7. It doesn’t matter if they’re lists, tuples, sets, strings, or byte strings:

>>>

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

All built-in Python objects that have a length follow this rule. Later, you’ll see some exceptions to this rule for non-built-in objects.

Other Types as Boolean Values

Unless types have a

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
51 or specifically define whether they’re truthy or falsy, they’re always truthy. This is true for built-in as well as user-defined types. In particular, functions are always truthy:

>>>

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

Methods are always truthy, too. You might encounter this if a parenthesis is missing when you call a function or method:

>>>

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

This can happen as a result of a forgotten parenthesis or misleading documentation that doesn’t mention that you need to call the function. If you expect a Python Boolean value but have a function that returns a Boolean value, then it will always be truthy.

By default, user-defined types are always truthy:

>>>

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

Creating an empty class makes every object of that class truthy. All objects are truthy unless special methods are defined. If you want to make some instances of your class falsy, you can define

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

>>>

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

You can also use

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55 to make an object neither truthy nor falsy:

>>>

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

The

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 statement also uses
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55. It does so to evaluate whether the object is truthy or falsy, which determines which branch to execute.

If you define the

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
59 method on a class, then its instances have a
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
51. In that case, the Boolean value of the instances will be falsy exactly when their length is
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7:

>>>

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

In this example,

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
62 would return
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7 before the assignment and
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
64 afterward. The reverse, however, is not true. Defining
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55 doesn’t give instances a length:

>>>

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

Defining

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55 doesn’t make instances of either class have a
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
51. When both
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55 and
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
69 are defined,
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
55 takes precedence:

>>>

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

Even though

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
82 has a length of
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
72, it’s still falsy.

Remove ads

Example: NumPy Arrays

The above example may seem like something that only happens when you write a class intended to demonstrate edge cases in Python. However, it’s possible to get similar results using one of the most popular libraries on PyPI: NumPy.

Arrays, like numbers, are falsy or truthy depending on how they compare to

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

>>>

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

Even though

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
82 has a length of
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5, it’s still falsy because its value is
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7.

When arrays have more than one element, some elements might be falsy and some might be truthy. In those cases, NumPy will raise an exception:

>>>

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

The exception is so wordy that in order to make it easy to read, the code uses text processing to wrap the lines.

An even more interesting edge case involves empty arrays. You might wonder if those are falsy like other sequences or truthy because they’re not equal to

>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7. As you saw above, those aren’t the only two possible answers. The arrays could also refuse to have a Boolean value.

Interestingly, none of these options is entirely true:

>>>

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

While empty arrays are currently falsy, relying on this behavior is dangerous. In some future NumPy version, this will raise an exception.

Operators and Functions

There are a few more places in Python where Boolean testing takes place. One of those is in Boolean operators.

The operators

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03, and
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 accept any value that supports Boolean testing. In the case of
>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33, it will always return a Boolean value:

>>>

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

The truth table for

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
33 is still correct, but now it takes the truthiness of the input.

In the case of

>>> bool

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67 and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03, in addition to short-circuit evaluation, they also return the value at which they stopped evaluating:

>>>

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

The truth tables are still correct, but they now define the truthiness of the results, which depends on the truthiness of the inputs. This can come handy when, for example, you want to give values defaults.

Assume you have a function called

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
85 that, if the text is too long, takes the beginning and the end and adds an ellipsis (
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
86) in the middle. This might be useful in some reports that can’t fit the full text. However, some datasets have missing values represented by
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
24.

Since

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
85 assumes the input is a string, it will fail on
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
24:

>>>

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

This example takes advantage of the falsiness of

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
24 and the fact that
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03 not only short-circuits but also returns the last value to be evaluated. The code for printing the report adds
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
92 to the argument to
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
85. The addition of
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
92 helps you to avoid errors with just a small code change.

The built-in functions

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
95 and
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
96 evaluate truthiness and also short-circuit, but they don’t return the last value to be evaluated.
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
95 checks whether all of its arguments are truthy:

>>>

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

In the last line,

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
95 doesn’t evaluate
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
99 for
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
5. Since
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
01 is
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7, this would have raised a
>>> False = 5
  File "", line 1
SyntaxError: cannot assign to False
61.

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
96 checks whether any of its arguments are truthy:

>>>

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

In the last line,

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
96 doesn’t evaluate
>>> lines="""\
... He took his vorpal sword in hand;
...       Long time the manxome foe he sought—
... So rested he by the Tumtum tree
...       And stood awhile in thought.
... """.splitlines()
>>> sum("the" in line.lower() for line in lines) / len(lines)
0.5
06 for
>>> def inverse_and_true(n):
...     1 // n
...     return True
...
>>> inverse_and_true(5)
True
>>> inverse_and_true(0)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in inverse_and_true
ZeroDivisionError: integer division or modulo by zero
>>> False and inverse_and_true(0)
False
7.

Remove ads

Conclusion

The Python Boolean is a commonly used data type with many useful applications. You can use Booleans with operators like

>>> bool

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

>>> bool = "this is not a type"
>>> bool
'this is not a type'
67,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
03,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
95,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
78,
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
55, and
>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
  File "", line 1
SyntaxError: cannot assign to True
56 to compare values and check for membership, identity, or equality. You can also use Boolean testing with an
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 statement to control the flow of your programs based on the truthiness of an expression.

In this tutorial, you’ve learned how to:

  • Manipulate Boolean values with Boolean operators
  • Convert Booleans to other types
  • Convert other types to Python Booleans
  • Use Booleans to write efficient and readable Python code

You now know how short-circuit evaluation works and recognize the connection between Booleans and the

>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0
12 statement. This knowledge will help you to both understand existing code and avoid common pitfalls that can lead to errors in your own programs.

Mark as Completed

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Booleans: Leveraging the Values of Truth

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Which of the following is not true about methods

Send Me Python Tricks »

About Moshe Zadka

Which of the following is not true about methods
Which of the following is not true about methods

Moshe has been using Python since 1998. He has contributed to CPython, and is a founding member of the Twisted project. He has been teaching Python in various venues since 2002.

» More about Moshe


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Which of the following is not true about methods

Aldren

Which of the following is not true about methods

Geir Arne

Which of the following is not true about methods

Jim

Which of the following is not true about methods

Joanna

Which of the following is not true about methods

Jacob

Master Real-World Python Skills With Unlimited Access to Real Python

Which of the following is not true about methods

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.

Which of the following is true about methods in Java?

Q) which of the following is true about methods in an interface in java? An interface can contain only abstract method.

Which of the following is not true for a constructor method?

What is false about constructor? Explanation: The constructor cannot have a return type. It should create and return new objects. Hence it would give a compilation error.

Which of the following is a true statement about passing data to a method?

27: Which of the following is a true statement about passing data to a method? A: A change made to a primitive value passed to a method is reflected in the calling method.

What is method in Java and its types?

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. Why use methods? To reuse code: define the code once, and use it many times.