[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
False
 >>> print 1 == 1 in word2
True
 >>> print 0 == 0 in word2
False
 >>> word2.append(0)
 >>> print 0 == 0 in word2
True
 >>>
> 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 
left-to-right.
 >>> print 0 in word2
True
 >>> 0 == True
False

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

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

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.
-Luke


More information about the Tutor mailing list