[Tutor] True/False evaluations?
Bob Gailer
bgailer at alum.rpi.edu
Wed Dec 1 19:35:41 CET 2004
At 05:51 AM 12/1/2004, Kent Johnson wrote:
>Liam Clarke wrote:
>>Hi all,
>>Got myself in a twist around logic of functions, I'll show you what I mean.
>>It's y() not being False (or True), but evaluating as true for 'if
>>not y()' which gets me, as I thought that [], 0, None, False, & {} all
>>evaluated the same.
>
>As you have discovered, an empty list is not the same as False, but it
>evaluates to False in conditionals. One way to think of it is, when you
>evaluate a conditional, the result is coerced to a boolean value. Numeric
>zeros and empty collections are coerced to False; everything else is True.
To be more complete and precise (from the language manual) "... when
expressions are used by control flow statements, the following values are
interpreted as false: None, numeric zero of all types, empty sequences
(strings, tuples and lists), and empty mappings (dictionaries). All other
values are interpreted as true."
Of course this is not precise in that "control flow statements" AFAIK
include if while for and try, but only in the context of if and while does
the above apply.
>[snip]
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell
More information about the Tutor
mailing list