Python中是否存在“不相等”运算符?

问题:Python中是否存在“不相等”运算符?

你怎么说不等于?

喜欢

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

是否有等同于==“不平等”的东西?

How would you say does not equal?

Like

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

Is there something equivalent to == that means “not equal”?


回答 0

使用!=。请参阅比较运算符。为了比较对象身份,可以使用关键字is及其否定词is not

例如

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

Use !=. See comparison operators. For comparing object identities, you can use the keyword is and its negation is not.

e.g.

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

回答 1

不等于 != (vs等于==

您是否在问这样的事情?

answer = 'hi'

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

Python-基本运算符图表可能会有所帮助。

Not equal != (vs equal ==)

Are you asking about something like this?

answer = 'hi'

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

This Python – Basic Operators chart might be helpful.


回答 2

当两个值不同时,有一个!=(不相等)运算符返回True,尽管要小心类型,因为"1" != 1"1" == 1由于类型不同,它将始终返回True,并且始终返回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.

There’s the != (not equal) operator that returns True when two values differ, though be careful with the types because "1" != 1. This will always return True and "1" == 1 will always return False, since the types differ. Python is dynamically, but strongly typed, and other statically typed languages would complain about comparing different types.

There’s also the else clause:

# 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"

The is operator is the object identity operator used to check if two objects in fact are the same:

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.

回答 3

您可以同时使用!=<>

但是,请注意,不建议!=<>不推荐的地方使用它。

You can use both != or <>.

However, note that != is preferred where <> is deprecated.


回答 4

看到其他所有人都已经列出了大多数其他方式来表示不平等,我将添加:

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)的检查切换为负,反之亦然…

Seeing as everyone else has already listed most of the other ways to say not equal I will just add:

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"

in this case it is simple switching the check of positive == (true) to negative and vise versa…


回答 5

您可以将“不等于”用于“不等于”或“!=“。请参见以下示例:

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

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

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

上面的代码将打印“不等于”,即早先分配的a = 2。

You can use “is not” for “not equal” or “!=”. Please see the example below:

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

The above code will print “true” as a = 2 assigned before the “if” condition. Now please see the code below for “not equal”

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

The above code will print “not equal” as a = 2 as assigned earlier.


回答 6

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

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

b。)<>如果两个操作数的值不相等,则条件变为true。(a <> b)是正确的。这类似于!=运算符。

There are two operators in Python for the “not equal” condition –

a.) != If values of the two operands are not equal, then the condition becomes true. (a != b) is true.

b.) <> If values of the two operands are not equal, then the condition becomes true. (a <> b) is true. This is similar to the != operator.


回答 7

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

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

Use != or <>. Both stands for not equal.

The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent. [Reference: Python language reference]


回答 8

您可以简单地执行以下操作:

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

You can simply do:

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