We've looked closely at Python's Boolean data type, which only has two values (True
and False
) and four operators: and
, or
, not
, and if-else
. The Boolean operators and the if
statement will both implicitly coerce values to a Boolean. This means that non-empty strings will behave in the same as the True
value.
We've looked at the comparison operators. These work with other objects and create Boolean results.
In the case of numeric comparisons, the numeric coercion rules are used to allow us to compare float
against int
values without having to write explicit conversions. For string or tuple values, we've seen that items are compared in order.
We've also seen how the logical operators of or
and and
are not strict about evaluating their operands. If the left-hand side of and
is False
, the right-hand side isn't evaluated. Similarly, if the left-hand side of or
is True
, the right-hand side isn't evaluated.
We looked at several kinds of Python statements, including the if-elif-else
statement...