【问题标题】:Is there a "not equal" operator in Python?Python中有“不等于”运算符吗?
【发布时间】:2012-06-19 02:29:48
【问题描述】:

你怎么说不等于?

喜欢

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

有没有等价于== 的意思是“不等于”?

【问题讨论】:

标签: python operators


【解决方案1】:

使用!=。见comparison operators。为了比较对象的身份,您可以使用关键字is 及其否定is not

例如

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

【讨论】:

  • 如何比较两个二进制数据?
  • 只是一些信息,cmets 中提到的 PEP401 是愚人节玩笑。 Python3 现在不支持<>
  • @LéoLéopoldHertz준영 不知道您可以比较二进制数据。我认为对象是您可以比较的最多的对象。
【解决方案2】:

不等于!=(对比等于==

你是在问这样的事情吗?

answer = 'hi'

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

这张Python - Basic Operators 图表可能会有所帮助。

【讨论】:

    【解决方案3】:

    !=(不等于)运算符在两个值不同时返回 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.
    

    【讨论】:

      【解决方案4】:

      您可以同时使用!=<>

      但是,请注意 != 是首选 <> 已弃用。

      【讨论】:

      • <> 在 Python 3 中不再存在,只能使用 !=
      【解决方案5】:

      鉴于其他人已经列出了大多数其他表示不相等的方式,我将添加:

      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"
      

      在这种情况下,将正 == (true) 的检查切换为负检查很简单,反之亦然...

      【讨论】:

        【解决方案6】:

        您可以将“is not”用于“不等于”或“!=”。请看下面的例子:

        a = 2
        if a == 2:
           print("true")
        else:
           print("false")
        

        上面的代码将打印“true”作为在“if”条件之前分配的a = 2。现在请看下面的“不等于”代码

        a = 2
        if a is not 3:
           print("not equal")
        else:
           print("equal")
        

        上面的代码将打印“不等于”作为前面分配的 a = 2。

        【讨论】:

        • 请注意is not 不适用于字符串比较
        • 感谢您的评论。这很有帮助!
        • 这个答案是完全错误的; isis not 不测试相等性,它们测试身份。
        • @kaya3,请帮助我理解为什么“不是”不适用于我的示例。
        • 我建议您阅读其他问答:stackoverflow.com/questions/13650293/…
        【解决方案7】:

        Python 中有两个运算符用于“不等于”条件 -

        a.) != 如果两个操作数的值不相等,则条件为真。 (a != b) 是真的。

        b.) 如果两个操作数的值不相等,则条件成立。 (a b) 是真的。这类似于 != 运算符。

        【讨论】:

          【解决方案8】:

          您可以使用!= 运算符来检查不等式。此外,在 python 2 中有 <> 运算符曾经做同样的事情,但在 python 3

          中已弃用

          【讨论】:

            【解决方案9】:

            使用!=<>。两者都代表不相等。

            比较运算符<>!= 是同一运算符的替代拼写。 != 是首选拼写; <> 已过时。 [参考:Python语言参考]

            【讨论】:

            • 这个答案基本上是@user128364之前给出的答案的副本。
            【解决方案10】:

            你可以这样做:

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

            【讨论】:

            • 您会为变量hibye 分配什么值?不管它是什么,elif 子句永远都达不到。最后,这个例子没有明确地回答这个问题。
            猜你喜欢
            • 2012-09-03
            • 1970-01-01
            • 2010-11-05
            • 2013-11-02
            • 2011-05-04
            • 2016-07-29
            • 2014-03-22
            • 2017-01-17
            • 2011-12-10
            相关资源
            最近更新 更多