True/false integer values

Andrew Wilkinson ajw126 at NOSPAMyork.ac.uk
Fri May 2 09:18:22 EDT 2003


A quick demonstration...

>>> ("", "s")[-1]
"s"

So the fact that it works doesn't mean that Python's true value is 1, having
said that though, try typing this into any Python pre 2.3 and you'll see
that it is actually 1.

>>> 1 == 1
1

In 2.3 the operators a being changed to return a boolean value, not an
integer. This new type is 'soft' type though as True + 1 == 2.

HTH,
Andrew Wilkinson

Carsten Gehling wrote:

> Page 515 of the Cookbook illustrates how the ternary operator
> 
> expression ? true-value : false-value
> 
> can be simulated in Python. The following example puzzles me:
> 
> for i in range(1, 3):
>     print "The loop ran %d time%s" % (i, ('', 's')[i != 1])
> 
> The tuple ('', 's') has indexes 0 and 1. Does that mean, that the boolean
> "true" value in Python is 1 and not -1?
> 
> - Carsten





More information about the Python-list mailing list