[Tutor] true and false
Bob Gailer
bgailer at alum.rpi.edu
Tue Aug 8 01:03:05 CEST 2006
Alan Gauld wrote:
>> What values represent true in Python?
>>
>
> True
>
> Its a built-in boolean type value.
>
> But empty strings, lists, tuples etc are considered False
> Similarly zero and None are considered False
>
Let us be precise here. Empty things are considered False when used
where a boolean value is expected.
In the language reference, under the if statement I quote:"It selects
exactly one of the suites by evaluating the expressions one by one until
one is found to be true (see section 5.10 <Booleans.html#Booleans> for
the definition of true and false)"
5:10: "In the context of Boolean operations, and also when expressions
are used by control flow statements, the following values are
interpreted as false: |False|, |None|, numeric zero of all types, and
empty strings and containers (including strings, tuples, lists,
dictionaries, sets and frozensets). All other values are interpreted as
true."
The 2 flow control statements that use expressions in this way are if
and while. Boolean operations are or, and, not.
So in "if [1]: print 1" [1] will be interpreted as True. However in
"[1] == True" it will NOT be interpreted as True.
--
Bob Gailer
510-978-4454
More information about the Tutor
mailing list