Logic operators β and, or, not, if-else
Python offers us four logical operators: and
, or
, not
, and if-else
. These work with Boolean values to create Boolean results. They're entirely distinct from the bit-wise operators of &
, |
, ^
, and ~
, that we looked at in Chapter 2, Simple Data Types.
The and
, or
, and not
operators are common in all programming languages. They fit the widely-used definitions from Boolean algebra.
The if-else
Boolean expression has three operands. In the middle, it uses a Boolean condition, but the other two operands can be objects of any types. Here's an example:
selection = "yankee" if wind < 15 else "stays'l"
The if-else
operator has a Boolean condition in the middle. In this example, it's the comparison, wind < 15
. If the condition is True
, then the left-most expression is the result, the string "yankee"
. If the condition is False
, then the right-most expression is the result; here, it's "stays...