Is there a difference between != and <> operators in Python?
34,035
The python documentation says that they are equivalent.
The comparison operators
<>
and!=
are alternate spellings of the same operator.!=
is the preferred spelling;<>
is obsolescent.
The <>
operator has been removed from Python 3.
Related videos on Youtube
Author by
Pa1
Updated on October 24, 2020Comments
-
Pa1 over 3 years
I tried searching but could not find much about the
<>
operator.https://www.tutorialspoint.com/python/python_basic_operators.htm mentions that
<>
is "similar" to the!=
operator and does not say what is different or how it is different.My tests seem to show it is the same:
a = 2, b = 3 >>> a != b True >>> a <> b True >>> b = 2 >>> a != b False >>> a <> b False
Any help to understand this would be appreciated.
-
ymonad over 7 years
<>
is deprecated and removed in python 3 stackoverflow.com/questions/11060506/…
-
-
T.J. Crowder over 7 years...and
<>
isn't mentioned in the v3 documentation at all. -
Pa1 over 7 years@ T.J. Crowder: Thanks for the clarification about the 2.x and 3.x difference as well.
-
sarnthil about 6 yearsyou can still use it Python 3:
from __future__ import barry_as_FLUFL
-
NoName over 4 yearsAny one know the historical reason for including <> as "not equal" in the first place? Never have I seen this operator before, in math or programming.
-
clemens over 4 years@NoName: Pascal uses <> as not equal operator, and it is simply a concatenation of lower and greater operator. You may read it as lower or greater than. This is in the context of Pascal equivalent to not equals. Many SQL dialects support both operators, too.