你怎么说不相等呢?

就像

if hi == hi:
    print "hi"
elif hi (does not equal) bye:
    print "no hi"

是否有等价于==的东西表示“不相等”?


当前回答

使用! =。参见比较运算符。对于比较对象标识,可以使用关键字is和其否定的is not。

e.g.

1 == 1 #  -> True
1 != 1 #  -> False
[] is [] #-> False (distinct objects)
a = b = []; a is b # -> True (same object)

其他回答

不相等!= (vs equal ==)

你是在问这种事吗?

answer = 'hi'

if answer == 'hi':     # equal
   print "hi"
elif answer != 'hi':   # not equal
   print "no hi"

这个Python -基本操作符图表可能会有帮助。

使用! =。参见比较运算符。对于比较对象标识,可以使用关键字is和其否定的is not。

e.g.

1 == 1 #  -> True
1 != 1 #  -> False
[] is [] #-> False (distinct objects)
a = b = []; a is b # -> True (same object)

使用!=或<>。Both代表不平等。

比较运算符<>和!=是同一运算符的交替拼写。!=是首选的拼写;<>是过时的。(参考:Python语言参考)

你可以简单地做:

if hi == hi:
    print "hi"
elif hi != bye:
     print "no hi"

有一个!=(不相等)操作符,当两个值不同时返回True,但要注意类型,因为“1”!= 1。这将总是返回True,而"1" == 1将总是返回False,因为类型不同。Python是动态的,但是是强类型的,而其他静态类型的语言会抱怨比较不同的类型。

还有else子句:

# This will always print either "hi" or "no hi" unless something unforeseen happens.
if hi == "hi":     # The variable hi is being compared to the string "hi", strings are immutable in Python, so you could use the 'is' operator.
    print "hi"     # If indeed it is the string "hi" then print "hi"
else:              # hi and "hi" are not the same
    print "no hi"

is操作符是对象标识操作符,用于检查两个对象实际上是否相同:

a = [1, 2]
b = [1, 2]
print a == b # This will print True since they have the same values
print a is b # This will print False since they are different objects.