[Tutor] trouble with function-- trying to check differences btwn 2 strings

Luke Paireepinart rabidpoobear at gmail.com
Tue Mar 6 15:18:42 CET 2007

David Perlman wrote:
> OK, I'm new to python too so I don't assume I know what I'm talking  
> about yet, but this looks like a mess to me.  What exactly does "item  
> == item in word2" evaluate to?  Does "in" or "==" have higher  
> precedence?
> I can't figure out how this would ever work at all.  It seems like  
> it's either checking to see whether boolean TRUE is in word2, or else  
> it's checking to see whether item is equal to boolean TRUE or FALSE,  
> and neither of those should ever be true.  
I'm not really sure what's happening, but check this out.
 >>> word2 = [1,2,3,4]
 >>> print 555 == 555 in word2
 >>> print 1 == 1 in word2
 >>> print 0 == 0 in word2
 >>> word2.append(0)
 >>> print 0 == 0 in word2
> And yet it seems to be  
> working out the same as "item in word2".
Yeah, it is working the same. 
It appears the == has higher precedence, or the operations are evaluated 
 >>> print 0 in word2
 >>> 0 == True

Which suggests what's really happening is
 >>> (0 == 0) in word2

So it's searching the list for anything that makes the condition true.
But that's not actually true.
 >>> (555 == 555) in word2
 >>> 555 == 555 in word2

So It appears to work differently depending on the circumstances.  In 
any event, it's not what the OP wanted.

Hope that wasn't confusing, and maybe helped.

More information about the Tutor mailing list