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

相关推荐

  • 基于ssm的个人博客系统的设计与实现(含源文件)

    基于ssm的个人博客系统的设计与实现(含源文件)欢迎添加微信互相交流学习哦!项目源码:https://gitee.com/oklongmm/biye进入二十一世纪,以Internet为核心的现代网络积水和通信技术已经得到了飞速的发展和广泛的应用,各种网络交流互动工具也应运而生。其中以论坛、博客、社区、空间最为受广大网民朋友的欢迎,也是目前为止发展的比较成熟的信息交流工具。在网络技术逐渐渗入社会生活各个层面的今天,传统的交流方式也面临着变革,而网络博客则是一个很重要的方向。基于Web技术的网络考试系统可以借助于遍布全球的Internet进行,因

    2022年7月21日
  • pycharm激活码2021_最新在线免费激活

    (pycharm激活码2021)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS3…

    2022年3月26日
  • 深入剖析Vue源码 – 组件基础[通俗易懂]

    深入剖析Vue源码 – 组件基础[通俗易懂]深入剖析Vue源码 – 组件基础

    2022年4月22日
  • intellij idea 配置svn及提交项目「建议收藏」

    intellij idea 配置svn及提交项目「建议收藏」一、intellijidea安装svn前提条件:你的电脑安装了小乌龟,也就是svn。1、配置svn如下图箭头所示,在安装TortoiseSVN的时候,默认commandlineclienttools,表示使用IDEa自带的svn。然后,选中你的svn安装的路径2、配置svn地址添加svn地址路径二、intellijidea提交项目1、…

    2022年5月14日
  • RGBD融合原理及实践[通俗易懂]

    RGBD融合原理及实践[通俗易懂]RGBD融合原理及实践前言原理部分实践前言好久没更新博客了,主要是因为懒,最近有些得闲,决定纪录下之前的工作。RT,RGBD数据融合其实就是将3D摄像机的RGB与Depth数据做融合显示的过程,做法也不难理解,就是将depthcamera与rgbcamera的像素对应起来即可。原理部分原理部分主要借鉴这篇博文,详细的公式在这就不作重复了,贴张图吧。从上面的博客或图片可以看出,…

    2022年9月17日
  • NN中常用的距离计算公式:欧式距离、曼哈顿距离、马氏距离、余弦、汉明距离[通俗易懂]

    NN中常用的距离计算公式:欧式距离、曼哈顿距离、马氏距离、余弦、汉明距离[通俗易懂]1、欧氏距离EuclideanDistance:2、曼哈顿距离Manhattan:3、Mahalanobis马氏距离马氏距离的浅显解释,见我的博文:https://blog.csdn.net/weixin_41770169/article/details/80759195马氏距离和欧式距离的对比,见我的博文:https://blog.csdn.net/we…

    2022年6月19日

发表回复

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

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