11

I come from a c style languages, so I am natural in using != as not equal, but when I came to Python, from the documentation I read, I learned that for this purpose the <> operator is used.

Recently, I have seen a lot of code using !=, so my question is if one of them is preferred over the other or is one of them deprecated.

Also, I would like to know if there is any difference between them.

coredump
  • 2,847
  • 5
  • 31
  • 51
  • which documentation did you read this in? – Andy Hayden Sep 10 '12 at 12:02
  • @hayden I don't really remember . I have found this on the official python documentation, I don't know if it was there, but now it is. http://docs.python.org/library/stdtypes.html – coredump Sep 10 '12 at 12:06
  • @AndyHayden: I also for some reason favored `<>` in my python because of some documentation or book I read and also don't remember which one. – User Jun 13 '14 at 19:35

4 Answers4

17

Python 2 supports both, in python 3 the <> operator has been removed.

There is no difference between the two, but != is the preferred form.

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
7

From the official docs you linked

!= can also be written <>, but this is an obsolete usage kept for backwards compatibility only. New code should always use !=.

I believe the rationale for originally accepting <> was that it looked more natural for someone coming from a mathematical background than the common C-style != operator.

chepner
  • 446,329
  • 63
  • 468
  • 610
4

I don't know what documentation you read, but I'm not aware of any that recommends <> over !=. PEP8, the main style guide, doesn't mention any such recommendation.

Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
1

Just for the record,<> has been obsolete since at least as early as version 1.4, which was released in October 1996.

user1071847
  • 541
  • 7
  • 18