Python的操作符!=和”is not”的区别是什么?
在Python中,!=被定义为不等于运算符。如果两边的操作数不相等,则返回true,如果相等则返回false。
>>> (10+2) != 12 # 两个表达式相同,因此返回false
False
>>> (10+2)==12
True
>>> 'computer' != "computer" # 两个字符串相等(单引号和双引号相同)
False
>>> 'computer' != "COMPUTER" # 大小写不同的字符串不相等
True
而is not运算符检查两个对象的id()是否相同。如果相同,则返回false;如果不同,则返回true。
>>> a=10
>>> b=a
>>> id(a), id(b)
(490067904, 490067904)
>>> a is not b
False
>>> a=10
>>> b=20
>>> id(a), id(b)
(490067904, 490068064)
>>> a is not b
True
更多Python相关文章,请阅读:Python 教程