news 2026/7/22 7:24:35

python不等于运算符的具体使用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python不等于运算符的具体使用

如果两个变量具有相同的类型并且具有不同的值 ,则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中不等于运算符的示例。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$ python2.7

Python2.7.10(default, Aug172018,19:45:58)

[GCC4.2.1Compatible Apple LLVM10.0.0(clang-1000.0.42)] on darwin

Type"help","copyright","credits"or"license"formore 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控制台的一些示例。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$ python3.7

Python3.7.0(v3.7.0:1bf9cc5093, Jun262018,23:26:24)

[Clang6.0(clang-600.0.57)] on darwin

Type"help","copyright","credits"or"license"formore information.

>>>10<>20

File"<stdin>", line1

10<>20

^

SyntaxError: invalid syntax

>>>10!=20

True

>>>10!=10

False

>>>'10'!=10

True

>>>

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字符串一起使用。

1

2

3

4

5

6

7

8

9

10

11

12

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 __()函数来实现这一点。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

classData:

id=0

record=''

def__init__(self, i, s):

self.id=i

self.record=s

def__ne__(self, other):

# return true if different types

iftype(other) !=type(self):

returnTrue

ifself.record !=other.record:

returnTrue

else:

returnFalse

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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/22 7:23:30

Spring Boot多数据源配置实战:Druid+MyBatisPlus整合指南

1. 项目概述在微服务架构盛行的当下&#xff0c;Spring Boot项目经常需要同时连接多个数据库实例。最近在重构一个老旧的会员系统时&#xff0c;就遇到了需要同时操作MySQL用户库和TiDB交易库的场景。经过多次踩坑&#xff0c;终于实现了基于DruidMyBatisPlus的稳定多数据源方案…

作者头像 李华
网站建设 2026/7/22 7:22:52

[Released] 4DGS Unity插件——免费的4D高斯溅射实时渲染方案

4DGS细节大升级这是一款支持4D Gaussian Splatting动态资产的Unity实时渲染插件&#xff0c;兼容URP渲染管线&#xff0c;支持PC与主流Android VR一体机&#xff08;Pico、Quest系列&#xff09;&#xff0c;完全免费。 用这个插件能得到什么&#xff1f; 如果你正在评估这个插…

作者头像 李华
网站建设 2026/7/22 7:20:35

国际品牌在华战略调整与零售生态变革分析

1. 国际品牌在华战略调整的深层逻辑Guess这个美国牛仔服饰品牌最近宣布退出中国零售市场&#xff0c;背后反映的是国际品牌在中国市场面临的战略困境。作为从业十余年的零售行业观察者&#xff0c;我亲历过多个国际品牌在华战略调整的完整周期。Guess此次退出并非孤例&#xff…

作者头像 李华
网站建设 2026/7/22 7:19:08

告别“治而不愈”,中翰软件用AI给数据治理开了一剂“新药”

数据治理&#xff0c;为何总陷入“投入大、效果差”的死循环&#xff1f;中翰软件最新推出的AI原生解决方案&#xff0c;直击痛点。 传统治理有五大顽疾&#xff1a;技术思维主导&#xff0c;业务人员看不懂数据&#xff1b;管理与需求脱节&#xff0c;问题难定位&#xff1b;事…

作者头像 李华
网站建设 2026/7/22 7:18:36

Java企业级AI视频数字人解决方案:JBoltAI核心技术解析

1. 项目概述&#xff1a;JBoltAI视频数字人解决方案在当今企业内容创作领域&#xff0c;视频制作已成为品牌传播的核心手段&#xff0c;但传统视频制作面临三大痛点&#xff1a;人力成本高、制作周期长、内容同质化严重。JBoltAI视频数字人解决方案正是针对这些痛点设计的Java企…

作者头像 李华
网站建设 2026/7/22 7:17:34

C++策略模式实战:从算法封装到架构优化的设计模式指南

1. 项目概述&#xff1a;为什么策略模式是C开发者的“瑞士军刀”&#xff1f;在C的世界里&#xff0c;我们常常会遇到这样的场景&#xff1a;一个核心的业务逻辑&#xff0c;比如数据排序、支付计算或者图像渲染算法&#xff0c;随着需求迭代&#xff0c;未来可能会有多种不同的…

作者头像 李华