At 12:50 AM 7/13/2008, Alan Gauld wrote:
"Dick Moores" <[EMAIL PROTECTED]> wrote
But why will a tuple with two elements will always evaluate to
True?

Thats the rule for evaluationg collections in Python.
An empty collection is False. Anything else is therefore true

Yes! A rule, not logic. I'm not contradicting Kent, just helping myself understand. First the rule, then logic in the application of the rule. And I assume the rule is there in Python because it makes things work better.

_Learning Python_  has an enlightening section, "The Meaning of True and False in Python", on p. 188 in the 3rd edition. I quote in part:

===================================
In Python, as in most programming languages, an integer 0 represents false, and an integer 1 represents true. In addition, though, Python recognizes any empty data structure as false, and any nonempty data structure as true. More generally, the notions of true and false are intrinsic properties of every object in Python -- each object is either true or false, as follows:

-- Numbers are true if nonzero.
-- Other objects are true if nonempty.
===================================

Martin Walsh introduced me to bool(). That was useful:

In [43]: bool([])
Out[43]: False

In [44]: bool([1,2])
Out[44]: True

In [45]: bool("")
Out[45]: False

In [46]: bool("foo")
Out[46]: True

In [48]: bool(())
Out[48]: False

In [49]: bool((0))
Out[49]: False

In [50]: bool((0,))
Out[50]: True

In [51]: bool([[]])
Out[51]: True

In [52]: bool("''")
Out[52]: True

In [53]: bool(None)
Out[53]: False

In [55]: bool(set())
Out[55]: False

In [56]: bool(set([]))
Out[56]: False

In [57]: bool([None])
Out[57]: True

In [58]: bool(0)
Out[58]: False

In [59]: bool(1)
Out[59]: True

In [60]: bool(87657865)
Out[60]: True

In [61]: bool(876.57865)
Out[61]: True

In [62]: bool(-876.57865)
Out[62]: True

And so it goes..

Dick

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to