`<>` 在 Python 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16749121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does `<>` mean in Python?
提问by michaelmeyer
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.:
我正在尝试在 Python 3.3 中使用一个旧库(可追溯到 2003 年!)。当我导入它时,Python 会抛出一个错误,因为<>源文件中有符号,例如:
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?
它到底是什么意思,我应该用哪个(最近的)标志替换它?
采纳答案by jamylak
It means not equal to. It was taken from ABC(python's predecessor) see here:
不等于的意思。它取自ABC(python 的前身),请参见此处:
x < y, x <= y, x >= y, x > y, x = y, x <> y, 0 <= d < 10Order tests (
<>means 'not equals')
x < y, x <= y, x >= y, x > y, x = y, x <> y, 0 <= d < 10订单测试(
<>表示“不等于”)
I believe ABCtook it from Pascal, a language Guido began programming with.
我相信ABC它来自 Pascal,这是 Guido 开始使用的一种语言。
It has now been removed in Python 3. Use !=instead. If you are CRAZYyou can scrap !=and allow only <>in Py3K using this easter egg:
它现在已在 Python 3 中删除。!=改为使用。如果你很疯狂,你可以使用这个复活节彩蛋废弃!=并只允许<>在 Py3K 中:
>>> from __future__ import barry_as_FLUFL
>>> 1 <> 2
True
回答by Peter Varo
It means NOT EQUAL, but it is deprecated, use !=instead.
这意味着 NOT EQUAL,但它已被弃用,请!=改用。
回答by lvc
It is an old way of specifying !=, that was removed in Python 3. A library old enough to use it likely runs into various otherincompatibilities with Python 3 as well: it is probably a good idea to run it through 2to3, which automatically changes this, among many other things.
这是一种旧的指定方式,!=在 Python 3 中被删除。一个足够老的库使用它也可能遇到与 Python 3 的各种其他不兼容问题:通过2to3运行它可能是一个好主意,它会自动改变这一点,等等。
回答by Colonel Panic
It's worth knowing that you can use Python itself to find documentation, even for punctuation mark operators that Google can't cope with.
值得知道的是,您可以使用 Python 本身来查找文档,即使是 Google 无法应对的标点符号运算符。
>>> 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 < chave the interpretation that is conventional in mathematics:Comparisons yield boolean values:
TrueorFalse.Comparisons can be chained arbitrarily, e.g.,
x < y <= zis equivalent tox < y and y <= z, except thatyis evaluated only once (but in both caseszis not evaluated at all whenx < yis 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.
比较
与 C 不同,Python 中的所有比较操作都具有相同的优先级,低于任何算术、移位或按位运算的优先级。同样与 C 不同的是,like 表达式
a < b < c具有数学中的常规解释:比较产生布尔值:
True或False。比较可以任意链接,例如,
x < y <= z等价于x < y and y <= z,除了y只计算一次(但在这两种情况下z,当x < y发现为假时根本不计算)。形式
<>和!=是等价的;为了与 C 一致,!=优先考虑;!=下面提到的地方<>也被接受。该<>拼写被认为是过时的。
See http://docs.python.org/2/reference/expressions.html#not-in
回答by Ehsan
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.
[参考:Python 语言参考] 比较运算符<>和!=是同一运算符的替代拼写。!=是首选拼写;<>已经过时了。

