在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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Flowable深入浅出-1 Flowable简介

    Flowable深入浅出-1 Flowable简介1Flowable简介什么是BPMN什么是FlowableFlowable官网、开源社区Flowable流程示例什么是BPMN先来看下百度百科的定义:由BPMI(TheBusinessProcessManagementInitiative)开发了一套标准叫业务流程建模符号(BPMN-BusinessProcessModelingNotation)。在BPMINotat…

    2022年5月21日
    103
  • 开源crm管理系统源码_客户关系管理的内容

    开源crm管理系统源码_客户关系管理的内容CRM管理系统源码,客户关系管理系统源码开发语言:PHP数据库:Mysql开发工具:phpstrom源码类型:WebForm系统功能简介:1、系统管理:1.部门管理2.岗位管理3.员工管理4.角色管理5.日志管理2、客户管理:客户管理:客户列表、客户联系人、服务记录 销售管理:销售机会、跟踪记录、产品报价、项目报备 合同管理:合同详细、新增合同 订单管理:广告订单、执行计划、订单执行、收款记录3、库存管理:1.入库管理…

    2022年9月16日
    0
  • SQL中的左连接与右连接,内连接有什么区别

    SQL中的左连接与右连接,内连接有什么区别例子,相信你一看就明白,不需要多说A表(a1,b1,c1)B表(a2,b2)左连接:selectA.*,B.*fromAleftouterjoinBon(A.a1=B.a2)结果是:右连接:selectA.*,B.*fromArightouterjoinBon(A.a1=B.a2)结果是:内连接:自然联结:…

    2022年9月19日
    0
  • 关于html5的PostMessage的用法总结

    关于html5的PostMessage的用法总结  大家都知道,网页直接传递数据可以使用ajax请求来完成,今天我总结下我学习的postMessage是如何完成跨页面请求数据的呢?首先,postMessage是html5新增的一个解决跨域的一个方法。那他是如何使用的呢?这里我把一个案例分享出来把。看下面的代码:test.html&lt;!DOCTYPEhtml&gt;&lt;html&gt;&lt;head&gt; &lt;titl…

    2022年7月13日
    14
  • Myeclipse注册码_为什么myeclipse

    Myeclipse注册码_为什么myeclipsemyeclipse6注册码生成最近由于项目使用eclipse+myeclipse开发工具开发,打开eclipse老是提示让激活myeclipse,需要注册码,于是去网上找了一些注册码来试,结果都不行,最后终于找到一篇文章介绍如何用Java程序生成注册码,参考百度经验原文:http://jingyan.baidu.com/article/a24b33cd53a9b819fe002ba5.html

    2022年9月29日
    0
  • hadoop启动HDFS命令「建议收藏」

    hadoop启动HDFS命令「建议收藏」启动命令:/hadoop/sbin/start-dfs.sh停止命令:/hadoop/sbin/stop-dfs.sh

    2022年10月5日
    0

发表回复

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

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