Python: Difference between != and "is not"

11,402

Solution 1

is tests for object identity, but == tests for object value equality:

In [1]: a = 3424
In [2]: b = 3424

In [3]: a is b
Out[3]: False

In [4]: a == b
Out[4]: True

Solution 2

is not compares references. == compares values

Solution 3

Depending on how you were confused, this might help.

These statements are the same:

[c for c in s if c != 'o']
[c for c in s if not c == 'o']

Solution 4

I'd like to add that they definitely do not do the same thing. I would use !=. For instance if you have a unicode string....

a = u'hello'
'hello' is not a
True
'hello' != a
False

With !=, Python basically performs an implicit conversion from str() to unicode() and compares them, whereas with is not, it matches if it is exactly the same instance.

Share:
11,402

Related videos on Youtube

Channel72
Author by

Channel72

Updated on May 30, 2022

Comments

  • Channel72
    Channel72 almost 2 years

    I'm unclear about the difference between the syntax != and is not. They appear to do the same thing:

    >>> s = 'a'
    >>> s != 'a'
    False
    >>> s is not 'a'
    False
    

    But, when I use is not in a list comprehension, it produces a different result than if I use !=.

    >>> s = "hello"
    >>> [c for c in s if c is not 'o']
    ['h', 'e', 'l', 'l', 'o']
    >>> [c for c in s if c != 'o']
    ['h', 'e', 'l', 'l']
    

    Why did the o get included in the first list, but not the second list?

    • user1066101
      user1066101 about 13 years
      When you looked at the Python language reference for these two operators, what did you see? This seems clear: docs.python.org/reference/expressions.html#notin. What part confused you?
    • michaelsnowden
      michaelsnowden
      This is really strange, I can't recreate this: Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> s = "hello" >>> [c for c in s if c is not 'o'] ['h', 'e', 'l', 'l'] >>> [c for c in s if c != 'o'] ['h', 'e', 'l', 'l']
  • ajwood
    ajwood about 13 years
    Why does a==123 return true after setting a=123?
  • Wilduck
    Wilduck about 13 years
    @ajwood: because they have the same value. == compares values.
  • Wilduck
    Wilduck about 13 years
    The reason is not worked for the strings in the op's example is an artifact of python's method of assigning strings to variables. Since strings are immutable, in the global namespace, two variables which contain the same string will be referencing the same object.
  • Wooble
    Wooble about 13 years
    A better question is why "a is 123" returns True in most implementations. (see stackoverflow.com/questions/306313/… )
  • eyquem
    eyquem about 13 years
    What sort of Python do you use ? With Python 2.7, I obtain True for the instruction a is b !
  • tkerwin
    tkerwin about 13 years
    This is using Python 2.6.4. The implementation can make the choice to cache certain objects. On my system, 'a' passes the is test, but 'apple sauce' does not.
  • ncoghlan
    ncoghlan about 13 years
    For the curious, that happens because 'a' looks like an identifier (so the interpreter caches it), but 'apple sauce' does not (due to the space), so if you want it cached you must do so explicitly via intern().
  • ajwood
    ajwood about 13 years
    @Wooble That is indeed that question I meant to ask