Truth Value Testing
Any
object can be tested for truth value, to use in an
if or while condition or as
operand of the boolean operations below. The following values are
considered false:
NoneZero of any numeric type, e.g.,
0,0L,0.0Any empty sequence, e.g., ‘ ’,
(),[]Any empty mapping, e.g.,
{}Instances of user-defined classes, if the class defines a
__nonzero__()or__len__()method, when that method returns zero
All other values are considered true, so objects
of many types are always true.
Operations and built-in functions that have a boolean result always
return
for false and 1 for true,
unless otherwise stated. Important exceptions are the boolean
operations or and and, which
always return one of their operands.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access