There are no explicit boolean types in Python. Any variable or expression can be tested for truth in an if statement. The rules for whether something is considered true or false are:
None
is false.
0
, 0L
, 0.0
, complex 0+0j
) are false.
""
, []
, ()
, and {}
are all false.
None
's,
for example, [None]
is true. ""
is false, but
the string "false"
is true).
fh = open('filename')
, the filehandle
fh
is true (even if the file is closed with
fh.close()
).
One cannot (mistakenly or deliberately) put an assignment statement inside a conditional as one can in C. For example the following is not legal Python:
But this is legal:
Questions or comments? Contact help@stsci.edu Documented updated on 2004 Jun 1 |