I know:
>>> 1 != 2
True
and:
>>> 1 <> 2
True
but I don't know what the difference between <>
and !=
I know:
>>> 1 != 2
True
and:
>>> 1 <> 2
True
but I don't know what the difference between <>
and !=
<>
is removed from the language in Python3.
In Python2, they are the same, but !=
is preferred.
Python 2.7 interprets the two statements exactly the same (as NOTEQUAL). See tokenizer.c.
Also from docs:
!=
can also be written<>
, but this is an obsolete usage kept for backwards compatibility only. New code should always use!=
.
(from https://docs.python.org/2/library/stdtypes.html#stdcomparisons)
How I read these statements
1 != 2
: I read this as 1 is not equal to 2.
Knowing that python supports 1 < 2 < 3
to express inequalities, you can consider <>
a shortcut for less than or greater than but not equal.
1 <> 2
: I read this as 1 is less than 2 exclusive AND 1 is greater than 2 exclusive which happens to exclude the case x==y where x = y.
They have the same functionality. != is used by convention. The only reason <> still exists is for backward compatibility with older versions of Python.
It is the same.
See the documentation: https://docs.python.org/2/library/stdtypes.html#stdcomparisons
!=
can also be written<>
, but this is an obsolete usage kept for backwards compatibility only. New code should always use!=
.