python 比较运算符

Python Comparison Operators are used to compare two objects. The output returned is a boolean value – True or False.

Python比较运算符用于比较两个对象。 返回的输出是布尔值– TrueFalse

Python比较运算符 (Python Comparison Operators)

There are 6 types of comparison operators in Python.

Python中有6种类型的比较运算符。

Comparison Operator Description Example
== returns True if two operands are equal, otherwise False. a == b
!= returns True if two operands are not equal, otherwise False. a != b
> returns True if left operand is greater than the right operand, otherwise False. a > b
< returns True if left operand is smaller than the right operand, otherwise False. a < b
>= returns True if left operand is greater than or equal to the right operand, otherwise False. a > b
<= returns True if left operand is smaller than or equal to the right operand, otherwise False. a < b
比较运算符 描述
== 如果两个操作数相等,则返回True,否则返回False。 a == b
!= 如果两个操作数不相等,则返回True,否则返回False。 a!= b
> 如果左操作数大于右操作数,则返回True,否则返回False。 a> b
< 如果左操作数小于右操作数,则返回True,否则返回False。 a <b
> = 如果左操作数大于或等于右操作数,则返回True,否则返回False。 a> b
<= 如果左操作数小于或等于右操作数,则返回True,否则返回False。 a <b

Python比较运算符示例 (Python Comparison Operators Example)

Let’s look at a simple example of using comparison operators with primitive data type such as an integer.

让我们看一个使用比较运算符和原始数据类型(例如整数)的简单示例。

>>> a = 10
>>> b = 10
>>> c = 20
>>>
>>> a == b
True
>>> a != b
False
>>> c > a
True
>>> c < a
False
>>> c <= 20
True
>>> c >= 20
True
>>>

Python Comparison Operator – Integer

Python比较运算符-整数

带字符串的Python比较运算符 (Python Comparison Operators with String)

The string is an object in Python programming. Let’s see whether comparison operators work with Strings or not.

该字符串是Python编程中的一个对象。 让我们看看比较运算符是否适用于字符串。

>>> # string comparison
>>> s1 = 'a'
>>> s2 = 'a'
>>> s3 = 'b'
>>> s1 == s2
True
>>> s1 != s2
False
>>> s1 > s3
False
>>> s1 < s3
True
>>> s1 <= s2
True
>>> s1 >= s2
True
>>>

Python Comparison Operators – String

Python比较运算符–字符串

那么这是否意味着比较运算符将可以与任何python对象一起使用? (So does it mean that comparison operators will work with any python objects?)

Let’s check it out by creating a custom class.

让我们通过创建一个自定义类来检查一下。

>>> class Data:pass>>> d1 = Data()
>>> d2 = Data()
>>> d1 == d2
False
>>> d1 != d2
True
>>> d1 > d2
Traceback (most recent call last):File "<pyshell#30>", line 1, in <module>d1 > d2
TypeError: '>' not supported between instances of 'Data' and 'Data'
>>> d1 < d2
Traceback (most recent call last):File "<pyshell#31>", line 1, in <module>d1 < d2
TypeError: '<' not supported between instances of 'Data' and 'Data'
>>> d1 <= d2
Traceback (most recent call last):File "<pyshell#32>", line 1, in <module>d1 <= d2
TypeError: '<=' not supported between instances of 'Data' and 'Data'
>>> d1 >= d2
Traceback (most recent call last):File "<pyshell#33>", line 1, in <module>d1 >= d2
TypeError: '>=' not supported between instances of 'Data' and 'Data'
>>>
>>>

Python Comparison Operator Overloading Error

Python比较运算符重载错误

为什么等于和不等于运算符起作用,而其他运算符则不起作用? (Why equals and not-equals operator worked but others didn’t?)

It’s because “object” is the base of every class in Python. And object provides an implementation of functions that are used for equals and not-equals operator.

这是因为“对象”是Python中每个类的基础。 对象提供了用于等于和不等于运算符的函数的实现。

比较运算符的功能 (Functions for Comparison Operators)

Here is the list of functions that are used by comparison operators. So if you want comparison operators to work with the custom object, you need to provide an implementation for them.

这是比较运算符使用的功能列表。 因此,如果希望比较运算符与自定义对象一起使用,则需要为其提供一个实现。

Comparison Operator Function
== __eq__(self, other)
!= __ne__(self, other)
> __gt__(self, other)
< __lt__(self, other)
>= __ge__(self, other)
<= __le__(self, other)
比较运算符 功能
== __eq __(自己,其他)
!= __ne __(自己,其他)
> __gt __(自己,其他)
< __lt __(自己,其他)
> = __ge __(自己,其他)
<= __le __(自己,其他)

Python比较运算符重载 (Python Comparison Operators Overloading)

Let’s look at an example to overload comparison operators in a custom object.

让我们看一个在自定义对象中重载比较运算符的示例。

# Learn how to override comparison operators for custom objectsclass Data:id = 0def __init__(self, i):self.id = idef __eq__(self, other):print('== operator overloaded')if isinstance(other, Data):return True if self.id == other.id else Falseelse:return Falsedef __ne__(self, other):print('!= operator overloaded')if isinstance(other, Data):return True if self.id != other.id else Falseelse:return Falsedef __gt__(self, other):print('> operator overloaded')if isinstance(other, Data):return True if self.id > other.id else Falseelse:return Falsedef __lt__(self, other):print('< operator overloaded')if isinstance(other, Data):return True if self.id < other.id else Falseelse:return Falsedef __le__(self, other):print('<= operator overloaded')if isinstance(other, Data):return True if self.id <= other.id else Falseelse:return Falsedef __ge__(self, other):print('>= operator overloaded')if isinstance(other, Data):return True if self.id >= other.id else Falseelse:return Falsed1 = Data(10)
d2 = Data(7)print(f'd1 == d2 = {d1 == d2}')
print(f'd1 != d2 = {d1 != d2}')
print(f'd1 > d2 = {d1 > d2}')
print(f'd1 < d2 = {d1 < d2}')
print(f'd1 <= d2 = {d1 <= d2}')
print(f'd1 >= d2 = {d1 >= d2}')

Output:

输出:

== operator overloaded
d1 == d2 = False
!= operator overloaded
d1 != d2 = True
> operator overloaded
d1 > d2 = True
< operator overloaded
d1 < d2 = False
<= operator overloaded
d1 <= d2 = False
>= operator overloaded
d1 >= d2 = True

摘要 (Summary)

Python comparison operators are used to compare two objects. We can easily implement specific functions to provide support for these operators for our custom objects.

Python比较运算符用于比较两个对象。 我们可以轻松地实现特定功能,以为我们的自定义对象的这些运算符提供支持。

翻译自: https://www.journaldev.com/26799/python-comparison-operators

python 比较运算符

python 比较运算符_Python比较运算符相关推荐

  1. python 对象的异或运算符_python的运算符

    算数运算符 算数运算符主要用作于计算机的算数运算 种类符号作用+加法.字符串的拼接 -减法 *乘法.字符串的重复 /除法 //地板除(除法) %取余(除法) **幂运算 +# 数字类型的加法运算 pr ...

  2. python快速运算符_Python基本运算符

    运算符是可以操纵操作数值的结构.如下一个表达式:10 + 20 = 30.这里,10和20称为操作数,+则被称为运算符. 运算符类型 Python语言支持以下类型的运算符 - 1.算术运算符 2.比较 ...

  3. python算术运算符_Python算术运算符及用法详解

    Python 支持所有的基本算术运算符,这些算术运算符用于执行基本的数学运算,如加.减.乘.除和求余等.下面是 7 个基本的算术运算符. +:加法运算符,例如如下代码:a = 5.2 b = 3.1 ...

  4. python位运算符_Python位运算符

    操作符1 名称:& 描述:按位与运算符 示例: #!/usr/bin/python # -*- coding: UTF-8 -*- # 定义变量,通过赋值运算符赋值"=" ...

  5. python中三元运算符_python 三元运算符详解

    python是没有三元描述符的,但是可以通过模拟的实现. 其中一种是: (X and V1) or V2 正常情况下是不会有错误的,但是文章中也提到了,当V1=""时,就会有问题 ...

  6. python语言有哪些关系运算符_python常用运算符有哪些?

    和其他大多数的语言一样,python 中常用的操作符也有算术操作符.比较操作符.逻辑操作符,但是又有一些差别,下面详细介绍. 1. 算术运算符 和其他大多数的语言一样,python 也有 (加).-( ...

  7. python 海象运算符_python := 海象运算符

    最近在做算法题 越来越发现python写法 真的挺好用的 记下来 map(lambda x: sum(x))  中 lambda代表匿名函数 re.findall(r'0+|1+',s)  是正则表达 ...

  8. python中移位运算符_python移位运算符

    1,二进制方式 >>> bin( 1)'0b1' >>> bin( 10)'0b1010' >>> a =0b10>>>a2 & ...

  9. python 除法取模_Python的运算符和表达式(上)

    上一篇文章霖小白分享了Python中的字符串和数字类型,这一篇让我们回到小学时代的数学,因为霖小白这一篇分享的是关于Python程序中的运算符和表达式,这一次先分享算术运算符和算术表达式,比较运算符和 ...

最新文章

  1. 2021-07-29 labelme注释、分类和Json文件转化(转化成彩图mask)
  2. 滤波、漫水填充、图像金字塔、图像缩放、阈值化
  3. 目前微服务/REST的最佳技术栈
  4. sicp第一章部分习题解答
  5. 1、MapReduce理论简介
  6. RocketMQ 主从同步机制
  7. 最简洁的方式,实现web端百度地图一键定位导航
  8. 如何用python获得实时股票信息_【python】用命令行获取实时股票信息
  9. python装b代码_教你装逼了:怎么样发布你的 Python 代码给别人 “pip install”
  10. 如何将立创元器件封装库导入AD使用
  11. Smali语法详解(2)
  12. java计算同比和环比
  13. 删除文件unlink
  14. 浙江大学计算机考研信息汇总
  15. 两台windows电脑互相备份
  16. 简单理解椭圆曲线的非对称加密应用
  17. Java源码阅读(类图自动生成工具)
  18. 数据库学习笔记04——关系数据库2
  19. Pytorch实现yolov3(train)训练代码详解(二)
  20. 基于大数据分析的安全管理平台技术研究及应用

热门文章

  1. 20135223何伟钦—信息安全系统设计基础第五周学习总结
  2. 数据库表多维度数据的计算和汇总
  3. [转载] python float()
  4. [转载] [Python图像处理] 二十二.Python图像傅里叶变换原理及实现
  5. [转帖]规模化敏捷-简要对比SAFe、LeSS和DAD模式
  6. Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用](秒杀imageloader)...
  7. 从零开始——基于角色的权限管理01(补充)
  8. QT5.1 调用https
  9. 跟我一起做面试题-linux线程编程(2)
  10. POI导入数据的过程中,遇到读取以科学计数法显示的数据