[Tutor] re weird bool

Alan Gauld alan.gauld at btinternet.com
Wed Feb 4 02:00:09 CET 2009


"prasad rao" <prasadaraon50 at 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 




More information about the Tutor mailing list