你怎么说不相等呢?

就像

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)

其他回答

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

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

鉴于其他人已经列出了大多数表示不平等的其他方式,我只想补充一句:

if not (1) == (1): # This will eval true then false
    # (ie: 1 == 1 is true but the opposite(not) is false)
    print "the world is ending" # This will only run on a if true
elif (1+1) != (2): #second if
    print "the world is ending"
    # This will only run if the first if is false and the second if is true
else: # this will only run if the if both if's are false
    print "you are good for another day"

在这种情况下,它是简单的切换检查positive == (true)到negative,反之亦然…

您可以同时使用!=或<>。

但是,请注意!=在<>被弃用的地方是首选的。

使用! =。参见比较运算符。对于比较对象标识,可以使用关键字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 -基本操作符图表可能会有帮助。