What is the difference between != and <>? [duplicate]

眉间皱痕 提交于 2019-12-01 15:16:25

问题


Perhaps this is a rather newbie-ish question, but I'm curious. I have tried searching for it, but I suppose I lack the correct terminology to search properly.

Difference between != and <>.

On searching again, "inequality", I found one that discusses not == and !=, but nothing about <>.


回答1:


In Python 2.x, <> is equivalent to !=, as described in the documentation:

The forms <> and != are equivalent; for consistency with C, != is preferred; where != is mentioned below <> is also accepted. The <> spelling is considered obsolescent.

In Python 3.x, <> has been removed. Again, the documentation says:

Removed Syntax

....

Removed <> (use != instead).




回答2:


They are interchangeable in Python 2, but <> is deprecated and has been removed in Python 3.

Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
>>> 1 <> 2
True
>>> 1 != 2
True

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
>>> 1 <> 2
  File "<stdin>", line 1
    1 <> 2
       ^
SyntaxError: invalid syntax
>>> 1 != 2
True



回答3:


Also if you want to use <> in Python 3.X, you can import this from future module.

Python 3.3.2
>>> from __future__ import barry_as_FLUFL
>>> 1<>1
False


来源:https://stackoverflow.com/questions/20792247/what-is-the-difference-between-and

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