What does `<>` mean in Python?

68,423

Solution 1

It means not equal to. It was taken from ABC (python's predecessor) see here:

x < y, x <= y, x >= y, x > y, x = y, x <> y, 0 <= d < 10

Order tests (<> means 'not equals')

I believe ABC took it from Pascal, a language Guido began programming with.

It has now been removed in Python 3. Use != instead. If you are CRAZY you can scrap != and allow only <> in Py3K using this easter egg:

>>> from __future__ import barry_as_FLUFL
>>> 1 != 2
  File "<stdin>", line 1
    1 != 2
       ^
SyntaxError: with Barry as BDFL, use '<>' instead of '!='
>>> 1 <> 2
True

Solution 2

It means NOT EQUAL, but it is deprecated, use != instead.

Solution 3

It's worth knowing that you can use Python itself to find documentation, even for punctuation mark operators that Google can't cope with.

>>> help("<>")

Comparisons

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

Comparisons yield boolean values: True or False.

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

The forms <> and != are equivalent; for consistency with C, != is preferred; where != is mentioned below <> is also accepted. The <> spelling is considered obsolescent.

See http://docs.python.org/2/reference/expressions.html#not-in

Solution 4

It is an old way of specifying !=, that was removed in Python 3. A library old enough to use it likely runs into various other incompatibilities with Python 3 as well: it is probably a good idea to run it through 2to3, which automatically changes this, among many other things.

Share:
68,423
michaelmeyer
Author by

michaelmeyer

Updated on July 05, 2022

Comments

  • michaelmeyer
    michaelmeyer almost 2 years

    I'm trying to use in Python 3.3 an old library (dating from 2003!). When I import it, Python throws me an error because there are <> signs in the source file, e.g.:

    if (cnum < 1000 and nnum <> 1000 and ntext[-1] <> "s":
        ...
    

    I guess it's a now-abandoned sign in the language.

    What exactly does it mean, and which (more recent) sign should I replace it with?

  • lvc
    lvc almost 11 years
    More than deprecated: in Python 3, it is a syntax error, as the questioner discovered.
  • Peter Varo
    Peter Varo almost 11 years
    Why would somebody add -1 for an accepted question? Comment please, so I could fix the problem, if there is any..
  • Bakuriu
    Bakuriu almost 11 years
    the __future__ import does not "get it back in Python3". It effectively replaces !=. After doing that import the expression 1 != 2 raises a SyntaxError.
  • glglgl
    glglgl almost 11 years
    @PeterVaro I upvoted yours, but in general (not in this case!) an answer being accepted is not a reason not to downvote it if the downvoter considers it bad and the accepting a bad desicion (again, which does not apply in this case).
  • glglgl
    glglgl almost 11 years
    That said, I as well would like to know the reason for the -1 in this case - the answer is perfectly valid and correct.
  • kirelagin
    kirelagin almost 11 years
    Actually, it's not historical. It was a 1st April joke. See PEP-401.
  • Alvin Wong
    Alvin Wong almost 11 years
  • TerryA
    TerryA almost 11 years
    This also appears in python 3 (well, something different for me). How strange. +1
  • John Y
    John Y about 7 years
    Importantly (as noted by one of the answers to Alvin Wong's link), the __future__ import only "works" at the REPL, not in a script.
  • Mark Ransom
    Mark Ransom almost 3 years
    @glglgl I've experienced this myself - someone feels the question shouldn't have been asked, and punishes anyone who leaves an answer even if the answer is correct. I don't believe in that myself, I reserve my downvotes for answers that are factually incorrect or actively harmful in some way.
  • Mark Ransom
    Mark Ransom almost 3 years
    This would be a great answer except that it appears it's as obsolete as the <> operator itself. @TerryA I can no longer find that text in Python 3.8.6.