千家信息网

python不等于运算符怎么用

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要为大家展示了"python不等于运算符怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"python不等于运算符怎么用"这篇文章吧。Pyth
千家信息网最后更新 2025年02月03日python不等于运算符怎么用

这篇文章主要为大家展示了"python不等于运算符怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"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)

OperatorDescription
!=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.7Python 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 darwinType "help", "copyright", "credits" or "license" for more information.>>> 10 <> 20True>>> 10 <> 10False>>> 10 != 20True>>> 10 != 10False>>> '10' != 10True>>>

Python 3示例 (Python 3 Example)

Here is some examples with Python 3 console.

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

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

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

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

x = 10y = 10z = 20 print(f'x is not equal to y = {x!=y}') flag = x != zprint(f'x is not equal to z = {flag}') # python is strongly typed languages = '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

以上是"python不等于运算符怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

运算符 运算 相同 示例 输出 不同 内容 函数 篇文章 类型 两个 变量 对象 学习 帮助 动态 可以通过 可在 字段 字符 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 入职计算机网络技术题库 给老师网络安全培训的目的 网络安全检查表打印 基于f框架的软件开发 怎么让玩家不进入服务器 网络安全零基础入门教程 江苏电商软件开发概况 西山居软件开发 网络安全漏洞管理利好飞利信 兴化seo优化网络技术好 华三的服务器 如何查询数据库表格项目平均值 无线网络技术创新服务 万国觉醒的地图服务器是什么意思 查询数据库表前多少行明细 电厂网络安全等级保护都做哪些 无法加入网络安全插画 服务器主板组阵列视频 优炫软件数据库安全 什么是高性能服务器 英雄联盟进服务器黑屏 数据库中怎么根据身份证查询年龄 安徽网络技术咨询创新服务 淘宝店铺适合做软件开发吗 网络安全工程师需要什么 数据库分页查询是干嘛的 万国觉醒的地图服务器是什么意思 静安区大规模软件开发厂家直销 学校网络安全排查汇报 格力做软件开发稳定么
0