[Tutor] re weird bool

2009-02-03 Thread prasad rao
helloyes.you are right.
 2==True
False

It is an unexpected result to me.
I thought any value other than 0 is True.
And the solution provided by you(bool(a/1))
is useful to me.

Thank you
Prasad
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] re weird bool

2009-02-03 Thread Alan Gauld


prasad rao prasadarao...@gmail.com wrote


2==True

False

It is an unexpected result to me.
I thought any value other than 0 is True.


Any value of non zero is treated as True in a boolean context.
But aq test of equality with a boolean value is not a boolean context.
For equiality you have to compare like with like, and you do that
by taking the boolean value of the number using bool()

So:

if 2: print 'True'
else: print 'False'

will print true
but

if 2 == True: print 'True'
else: print 'False'

will print False.
The two tests are not identical.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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