Python中有“不相等”的运算符吗?

本文翻译自:Is there a “not equal” operator in Python?

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"? 有没有相当于==东西意味着“不平等”?


#1楼

参考:https://stackoom.com/question/kPLG/Python中有-不相等-的运算符吗


#2楼

Use != . 使用!= See comparison operators . 查看比较运算符 For comparing object identities, you can use the keyword is and its negation is not . 对于比较对象标识,您可以使用关键字is而其否定is not

eg 例如

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

#3楼

Not equal != (vs 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. 这个Python - Basic Operators图表可能会有所帮助。


#4楼

There's the != (not equal) operator that returns True when two values differ, though be careful with the types because "1" != 1 . 当两个值不同时,有!= (不等于)运算符返回True ,但要注意类型,因为"1" != 1 This will always return True and "1" == 1 will always return False, since the types differ. 这将始终返回True, "1" == 1将始终返回False,因为类型不同。 Python is dynamically, but strongly typed, and other statically typed languages would complain about comparing different types. Python是动态的,但强类型,其他静态类型的语言会抱怨比较不同的类型。

There's also the else clause: 还有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"

The is operator is the object identity operator used to check if two objects in fact are the same: 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.

#5楼

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... 在这种情况下,简单地将正==(真)的检查切换为否定,反之亦然......


#6楼

You can simply do: 你可以简单地做:

if hi == hi:
    print "hi"
elif hi != bye:
     print "no hi"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值