Does '!=' equivelent to 'is not'

Erik Max Francis max at alcyone.com
Tue Jun 17 05:41:49 CEST 2008


pirata wrote:

> I'm a bit confusing about whether "is not" equivelent to "!="
> 
> if a != b:
>   ...
> 
> if a is not b:
>   ...
> 
> 
> What's the difference between "is not" and "!=" or they are the same thing?

The `==` operator tests equality.  The `is` operator tests identity.

If you don't specifically intend to test for identity, use `==`.  If you 
don't know what identity tests are for (with the exception of testing 
for None-ness), then you don't need it.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
   Do we ask what profit the little bird hopes for in singing?
    -- Johannes Kepler, 1571-1630



More information about the Python-list mailing list