Python's logical operators: and
, or
and not
are special in control flow — they disrupt the way Python reads a program. In this guidebook, we'll discuss how it does so.
In Python, some values are truth-y while others are false-y. Here is a non-exhaustive list of some values.
False-y: False, 0, None, [], {}, ""
Truth-y: True, 1, 2, [1], "61a", {"one": 2}
not
The not
operator does not change the way control flows, which is why we discuss it first.
It changes a Truth-y value to a False-y one, and vice-versa.
>>> not True
False
>>> not False
True
>>> not "61a"
False
>>> not []
True
>>> not (200 == 300)
True
or
The or
operator returns the first Truth-y value it finds, or — in case no Truth-y value is found — the last False-y value it finds.
>>> 1 or 2
1
>>> 0 or 1
1
>>> 0 or None
None
>>> 0 or None or 1
1