[Tutor] New to programming question (Ben M.) (Joseph Q.)

Brian van den Broek bvande at po-box.mcgill.ca
Thu Apr 14 19:31:51 CEST 2005


Joseph Quigley said unto the world upon 2005-04-14 10:46:

<SNIP>

>  >Do you see?  The == "binds more tightly" than the or.  And, in python, 
> 'Q' is
>  >considered True for the purposes of tests.

<SNIP>

>  >What you can do instead is this:
>  >
>  >for letter in prefixes:
>  >    if letter in ['O', 'Q']:
>  >        print letter + 'u' + suffix
>  >    else:
>  >        print letter + suffix
> 
> Oh, ok. Sorry, my bad :)
> So there is a special use for ==. I must look into this.
> Thanks,
>     Joe

Hi Joe,

I'm not sure what you mean by a "special use", but maybe this will help:

In arithmetic:

4 * 10 + 2 = 42

as

( 4 * 10 + 2 ) = ( ( 4 * 10 ) + 2 )

This is because '*' comes before '+' in the oder of operations when 
parenthesis don't settle the issue. One way to express that is to say 
"'*' binds more tightly than '+'".

In Python '==' binds more tightly than 'or'. So:

( a == b or c ) == ( ( a == b ) or c )

Hence:
 >>> 42==0 or True
True
 >>> 42 == 0 or 42 == True
False
 >>> # Better is
 >>> 42 in (0, True)
False
 >>>

Does that help?

Best,

Brian vdB



More information about the Tutor mailing list