[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:
> >Do you see? The == "binds more tightly" than the or. And, in python,
> 'Q' is
> >considered True for the purposes of tests.
> >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.
I'm not sure what you mean by a "special use", but maybe this will help:
4 * 10 + 2 = 42
( 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 )
>>> 42==0 or True
>>> 42 == 0 or 42 == True
>>> # Better is
>>> 42 in (0, True)
Does that help?
More information about the Tutor