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
:
None
Zero of any numeric type, e.g.,
0
,0L
,0.0
Any 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.