什么 != 在 python 中做/意味着什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22209729/
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 != do/mean in python
提问by Bob Unger
I see this code in my python reference guide, but no description.
我在我的 python 参考指南中看到了这段代码,但没有描述。
I am asked a question like what does this do:
我被问到一个问题,比如这有什么作用:
print 2 and 1-2 !=3
It returns True, but why?
它返回True,但为什么呢?
采纳答案by Brian English
!=means "not equal to" and is a logical comparison. Break down the logical expression here:
!=表示“不等于”并且是逻辑比较。在这里分解逻辑表达式:
2 and 1 - 2 != 3
2 and -1 != 3
2 and True
True
回答by jimm-cl
Is a comparison operator. You can review the following link for details:
是一个比较运算符。您可以查看以下链接了解详情:
http://docs.python.org/2/reference/lexical_analysis.html#operators
http://docs.python.org/2/reference/lexical_analysis.html#operators
It means not equal. Also, from the same page:
这意味着不相等。此外,从同一页面:
The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling
比较运算符 <> 和 != 是同一运算符的替代拼写。!= 是首选拼写
回答by avoid3d
The operator '!=' in python takes the thing on the left hand side of itself and the thing on the right hand side of itself, and returns True if they are not equal, and false if they are equal.
python中的运算符'!='取自身左侧的事物和自身右侧的事物,如果它们不相等则返回True,如果相等则返回false。
(a != b)is the same as (not (a==b))
(a != b)是相同的 (not (a==b))
Your expression 2 and 1 - 2 != 3 gets evaluated like this:
你的表达式 2 和 1 - 2 != 3 得到这样的评估:
1) 2 and 1 - 2 != 3
2) 2 and -1 != 3
3) 2 and True
4) True
回答by tismle
!= checks if the value of two operands are equal, if values are not equal than the condition becomes true.
!= 检查两个操作数的值是否相等,如果值不相等则条件变为真。
example:
例子:
if a = 10, b = 20
(a != b) is true
如果 a = 10, b = 20
(a != b) 是真的
http://www.tutorialspoint.com/python/python_basic_operators.htm
http://www.tutorialspoint.com/python/python_basic_operators.htm
回答by Lil Taco
!= is essentially the same as:
!= 本质上与:
print(not 5 == 6)
print(5 != 6)

