53

I notice there is a comparison operator is not. Should I literally translate it into

!=

instead of

== not

?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nos
  • 19,875
  • 27
  • 98
  • 134
  • http://docs.python.org/reference/expressions.html#notin – Ignacio Vazquez-Abrams Dec 19 '10 at 21:36
  • The link in the venerable Ignatio's comment is broken, it should read: http://docs.python.org/reference/expressions.html#not-in (added a hyphen in the anchor) – Jonathan Hartley Mar 08 '16 at 15:12
  • 2
    I think it should be https://docs.python.org/3/reference/expressions.html#is-not – flow2k Jan 08 '20 at 22:20
  • Related (could be closer to the intended for some search engine queries): *[Python != operation vs "is not"](https://stackoverflow.com/questions/2209755/python-operation-vs-is-not)*. Which is a duplicate of *[Why does comparing strings using either '==' or 'is' sometimes produce a different result?](https://stackoverflow.com/questions/1504717/why-does-comparing-strings-using-either-or-is-sometimes-produce-a-differe)* – Peter Mortensen Jan 06 '23 at 18:13

4 Answers4

70

To expand on what Ignacio said:

a == b and a != b test whether two objects have the same value. You can override an object's __eq__ and __ne__ methods to determine what that means.

a is b and a is not b test whether two objects are the same thing. It's like doing id(a) == id(b).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • 1
    except without the race condition – Lie Ryan Dec 19 '10 at 22:32
  • @Lie Ryan: Care to elaborate? I don't know about that. – Thomas K Dec 19 '10 at 23:58
  • 13
    see: http://stackoverflow.com/questions/2906177/what-is-the-difference-between-a-is-b-and-ida-idb-in-python, especially Mike Graham's answer about `id(a.test) == id(b.test)` and `a.test is b.test`; to paraphrase, `id(a) == id(b)` does not guarantee that `a` and `b` are still alive when the comparison is made, and could cause some surprises when `a` is garbage collected before the comparison and `b` is created just then. – Lie Ryan Dec 20 '10 at 03:47
  • 9
    Interesting to be reminded that this breaks my normal mental rule of thumb that "not X" has a higher precedence than everything else. It is parsed as "A (is not) B" instead, where "is not" is an atomic operator. – Jonathan Hartley Mar 04 '16 at 14:31
  • You say "whether two objects are the same thing". What is that thing? – carloswm85 May 08 '20 at 23:22
  • 2
    @JonathanHartley `a is not b` cannot be parsed as `a is (not b)` because the `is` operator has a higher precedence than the `not` operator in python. So the only possible way to parse it is as `is not`. That's also why `a == not b` is a syntax error and `not a == b` is parsed as `not (a == b)`. – Robert Jul 17 '20 at 13:03
20

It's not a relational comparison; it's identity. And it translates to not (A is B).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
7

Python 2.7.3 documentation, 5.9. Comparisons:

The operators <, >, ==, >=, <=, and != compare the values of two objects.

and about operator is in the same chapter:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2r2w
  • 1,384
  • 1
  • 12
  • 29
1
A != B

means that "A is not equal to B", not "A is equal to not B".

zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • What's the difference? If A is not equal to B, A would have to be equal to *something* let's assume A equals C. That means C does not equal B. Therefore, A is equal to not B(C). – John Dec 19 '10 at 22:09
  • 1
    @John: "A is equal to C", "A is not equal to B", and "C is not equal to B" does not imply that "(not(B)) is equal to C"; it is a faulty reasoning to conclude from this that "A is equal to not B". – Lie Ryan Dec 19 '10 at 22:41
  • 2
    This comment is based on a misunderstanding of the question. "A is not B" is not parsed as "A is (not B)". It is parsed as "A (is not) B", where "is not" is an operator that tests for "not (A is B)". – Jonathan Hartley Mar 04 '16 at 14:28
  • @John : For (A,B)=(2,0), "A is not equal to B" is True but "A is equal to not B" is False. So A != B is definitely not the same as A == (not B). – Max May 23 '22 at 14:35