在Python中,!=定义为不等于运算符。如果两端的操作数彼此不相等,则返回true;如果相等,则返回false。
>>> (10+2) != 12 # both expressions are same hence false False >>> (10+2)==12 True >>> 'computer' != "computer" # both strings are equal(single and double quotes same) False >>> 'computer' != "COMPUTER" #upper and lower case strings differ True
然而,不是运算符检查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