What does `<>` mean in Python?
Question:
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?
Answers:
It means NOT EQUAL, but it is deprecated, use !=
instead.
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
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.
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
Use !=
or <>
. Both stands for not equal.
[Reference: Python language reference]
The comparison operators <>
and !=
are alternate spellings of the same operator. !=
is the preferred spelling; <>
is obsolescent.
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?
It means NOT EQUAL, but it is deprecated, use !=
instead.
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
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.
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 likea < b < c
have the
interpretation that is conventional in mathematics:Comparisons yield boolean values:
True
orFalse
.Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is
equivalent tox < y and y <= z
, except thaty
is evaluated
only once (but in both casesz
is not evaluated at all whenx <
is found to be false).
yThe 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
Use !=
or <>
. Both stands for not equal.
[Reference: Python language reference]
The comparison operators <>
and !=
are alternate spellings of the same operator. !=
is the preferred spelling; <>
is obsolescent.