在Python中关系运算符中,表示“不等于”(python的逻辑运算符)

python不等于运算符PythonnotequaloperatorreturnsTrueiftwovariablesareofsametypeandhavedifferentvalues,ifthevaluesaresamethenitreturnsFalse.如果两个变量具有相同的类型并且具有不同的值,则Python不等于运算符将返回Tru…

大家好,又见面了,我是你们的朋友全栈君。

python不等于运算符

Python not equal operator returns True if two variables are of same type and have different values, if the values are same then it returns False.

如果两个变量具有相同的类型并且具有不同的值 ,则Python不等于运算符将返回True ;如果值相同,则它将返回False

Python is dynamic and strongly typed language, so if the two variables have the same values but they are of different type, then not equal operator will return True.

Python是动态的强类型语言,因此,如果两个变量具有相同的值,但它们的类型不同,则不相等的运算符将返回True

Python不等于运算符 (Python not equal operators)

Operator Description
!= Not Equal operator, works in both Python 2 and Python 3.
<> Not equal operator in Python 2, deprecated in Python 3.
操作员 描述
!= 不是Equal运算符,可在Python 2和Python 3中使用。
<> 在Python 2中不等于运算符,在Python 3中已弃用。

Python 2示例 (Python 2 Example)

Let’s see some examples of not-equal operator in Python 2.7.

我们来看一些Python 2.7中不等于运算符的示例。

$ python2.7
Python 2.7.10 (default, Aug 17 2018, 19:45:58) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 <> 20
True
>>> 10 <> 10
False
>>> 10 != 20
True
>>> 10 != 10
False
>>> '10' != 10
True
>>>

Python 3示例 (Python 3 Example)

Here is some examples with Python 3 console.

这是Python 3控制台的一些示例。

$ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 <> 20
  File "<stdin>", line 1
    10 <> 20
        ^
SyntaxError: invalid syntax
>>> 10 != 20
True
>>> 10 != 10
False
>>> '10' != 10
True
>>>

We can use Python not equal operator with f-strings too if you are using Python 3.6 or higher version.

如果您使用的是Python 3.6或更高版本,我们也可以将Python不等于运算符与f字符串一起使用。

x = 10
y = 10
z = 20

print(f'x is not equal to y = {x!=y}')

flag = x != z
print(f'x is not equal to z = {flag}')

# python is strongly typed language
s = '10'
print(f'x is not equal to s = {x!=s}')

Output:

输出:

x is not equal to y = False
x is not equal to z = True
x is not equal to s = True

Python不等于自定义对象 (Python not equal with custom object)

When we use not equal operator, it calls __ne__(self, other) function. So we can define our custom implementation for an object and alter the natural output.

当我们使用不等于运算符时,它将调用__ne__(self, other)函数。 因此,我们可以为对象定义自定义实现并更改自然输出。

Let’s say we have Data class with fields – id and record. When we are using the not-equal operator, we just want to compare it for record value. We can achieve this by implementing our own __ne__() function.

假设我们有带字段的Data类-id和record。 当我们使用不等于运算符时,我们只想比较它的记录值。 我们可以通过实现自己的__ne __()函数来实现这一点。

class Data:
    id = 0
    record = ''

    def __init__(self, i, s):
        self.id = i
        self.record = s

    def __ne__(self, other):
        # return true if different types
        if type(other) != type(self):
            return True
        if self.record != other.record:
            return True
        else:
            return False


d1 = Data(1, 'Java')
d2 = Data(2, 'Java')
d3 = Data(3, 'Python')

print(d1 != d2)
print(d2 != d3)

Output:

输出:

False
True

Notice that d1 and d2 record values are same but “id” is different. If we remove __ne__() function, then the output will be like this:

请注意,d1和d2记录值相同,但“ id”不同。 如果删除__ne __()函数,则输出将如下所示:

True
True
GitHub Repository.
GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/25101/python-not-equal-operator

python不等于运算符

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125499.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数字金额大小写转换

    数字金额大小写转换

    2021年12月17日
    39
  • 微信小程序发送消息推送_小程序推送消息设置

    微信小程序发送消息推送_小程序推送消息设置在小程序开发中,如果想实现:用户发给小程序的消息以及开发者需要的事件推送,在小程序项目中,我们想要实现这样一个功能,比如我们小程序中的客服功能,我不想要使用小程序后台的在线客服功能,但我又想实现客服功能,这个时候微信提供了消息推送功能,在小程序后台的设置,开发设置中,消息推送功能:点击开启消息推送功能,认证成功进入到消息推送配置界面:这里配置几个参数注意一下:URL(服务器………

    2022年10月7日
    4
  • c 语言对gis导航二次开发,arcgis二次开发_arcgis二次开发语言_arcgis二次开发实例…[通俗易懂]

    c 语言对gis导航二次开发,arcgis二次开发_arcgis二次开发语言_arcgis二次开发实例…[通俗易懂]arcgis二次开发arcgis二次开发Arcgis二次开发常用源代码1.点上生成面的代码if(m_pFeatureLayer.FeatureClass.ShapeType==esriGeometryType.esriGeometryPolygon){IPointCollectionm_pPointCollection=newPolygonClass();objectmiss…

    2022年6月17日
    26
  • w7812三端稳压电路图_acwing是什么

    w7812三端稳压电路图_acwing是什么达达是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女翰翰,从而被收留在地球上。翰翰的家里有一辆飞行车。有一天飞行车的电路板突然出现了故障,导致无法启动。电路板的整体结构是一个 R 行 C 列的网格(R,C≤500),如下图所示。每个格点都是电线的接点,每个格子都包含一个电子元件。电子元件的主要部分是一个可旋转的、连接一条对角线上的两个接点的短电缆。在旋转之后,它就可以连接另一条对角线的两个接点。电路板左上角的接点接入直流电源,右下角的接点接入飞行车的发动装置。达达发现因为

    2022年8月9日
    10
  • matlab怎么fprintf,matlab中的fprintf函数怎么用「建议收藏」

    matlab怎么fprintf,matlab中的fprintf函数怎么用「建议收藏」matlab中的fprintf函数怎么用发布时间:2020-06-2217:10:46来源:亿速云阅读:97作者:Leahmatlab中的fprintf函数怎么用?针对这个问题,今天小编总结了这篇文章,希望能帮助更多想解决这个问题的朋友找到更加简单易行的办法。fprintf函数可以将数据按指定格式写入到文本文件中。其调用格式为:数据的格式化输出:fprintf(fid,format,vari…

    2022年8月31日
    4
  • Animator_制作动画的软件

    Animator_制作动画的软件1、ApplyRootMotionAnimatorMoveScript.cs:voidOnAnimatorMove(){//在脚本中使用此函数,并把它添加到相应对象上//这个方法就会覆盖Ap

    2022年8月2日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号