What does `<>` mean in Python?

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

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?

回答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 True 


回答2:

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



回答3:

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.



回答4:

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



回答5:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!