[Tutor] true and false

wesley chun wescpy at gmail.com
Mon Aug 7 21:48:12 CEST 2006


> What values represent true in Python?

good question.  let's look at it from the other way as it's easier:

a Boolean False value is any zero or empty container while True is
anything *other* than those values.  at the risk of plagarizing
myself, here is a clip from chapter 4 of "Core Python:"

CORE NOTE: Boolean values

All standard type objects can be tested for truth value and compared
to objects of the same type. Objects have inherent True or False
values. Objects take a False
value when they are empty, any numeric representation of zero, or the
Null object,
None. The following are defined as having false values in Python:

• None
• False (Boolean)
• 0 (integer)
• 0.0 (float)
• 0L (long integer)
• 0.0+0.0j (complex)
• "" (empty string)
• [] (empty list)
• () (empty tuple)
• {} (empty dictionary)
(• other empty objects)

Any value for an object other than the those above is considered to
have a true value, i.e., non-empty, non-zero, etc. User-created class
instances have a false value when their nonzero (__nonzero__()) or
length (__len__()) special methods, if defined, return a zero value.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list