首页 > 编程知识 正文

python 比较运算符Python比较运算符

时间:2023-05-04 11:10:26 阅读:248471 作者:3776

python 比较运算符

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

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

Python比较运算符 (Python Comparison Operators)

There are 6 types of comparison operators in Python.

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

Comparison OperatorDescriptionExample==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 == bTrue>>> a != bFalse>>> c > aTrue>>> c < aFalse>>> c <= 20True>>> c >= 20True>>>

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 == s2True>>> s1 != s2False>>> s1 > s3False>>> s1 < s3True>>> s1 <= s2True>>> s1 >= s2True>>>

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 == d2False>>> d1 != d2True>>> d1 > d2Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> d1 > d2TypeError: '>' not supported between instances of 'Data' and 'Data'>>> d1 < d2Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> d1 < d2TypeError: '<' not supported between instances of 'Data' and 'Data'>>> d1 <= d2Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> d1 <= d2TypeError: '<=' not supported between instances of 'Data' and 'Data'>>> d1 >= d2Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> d1 >= d2TypeError: '>=' 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 OperatorFunction==__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 = 0 def __init__(self, i): self.id = i def __eq__(self, other): print('== operator overloaded') if isinstance(other, Data): return True if self.id == other.id else False else: return False def __ne__(self, other): print('!= operator overloaded') if isinstance(other, Data): return True if self.id != other.id else False else: return False def __gt__(self, other): print('> operator overloaded') if isinstance(other, Data): return True if self.id > other.id else False else: return False def __lt__(self, other): print('< operator overloaded') if isinstance(other, Data): return True if self.id < other.id else False else: return False def __le__(self, other): print('<= operator overloaded') if isinstance(other, Data): return True if self.id <= other.id else False else: return False def __ge__(self, other): print('>= operator overloaded') if isinstance(other, Data): return True if self.id >= other.id else False else: 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 overloadedd1 == d2 = False!= operator overloadedd1 != d2 = True> operator overloadedd1 > d2 = True< operator overloadedd1 < d2 = False<= operator overloadedd1 <= d2 = False>= operator overloadedd1 >= 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 比较运算符

历史中提交的图片或压缩文件javascript中有哪些类型

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。