python 比较运算符
Python Comparison Operators are used to compare two objects. The output returned is a boolean value – True
or False
.
Python比较运算符用于比较两个对象。 返回的输出是布尔值– True
或False
。
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 |
> = | 如果左操作数大于或等于右操作数,则返回True,否则返回False。 | a> b |
<= | 如果左操作数小于或等于右操作数,则返回True,否则返回False。 | a |
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
>>>
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
>>>
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 "", line 1, in
d1 > d2
TypeError: '>' not supported between instances of 'Data' and 'Data'
>>> d1 < d2
Traceback (most recent call last):
File "", line 1, in
d1 < d2
TypeError: '<' not supported between instances of 'Data' and 'Data'
>>> d1 <= d2
Traceback (most recent call last):
File "", line 1, in
d1 <= d2
TypeError: '<=' not supported between instances of 'Data' and 'Data'
>>> d1 >= d2
Traceback (most recent call last):
File "", line 1, in
d1 >= d2
TypeError: '>=' not supported between instances of 'Data' and 'Data'
>>>
>>>
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中每个类的基础。 对象提供了用于等于和不等于运算符的函数的实现。
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 __(自己,其他) |
Let’s look at an example to overload comparison operators in a custom object.
让我们看一个在自定义对象中重载比较运算符的示例。
# Learn how to override comparison operators for custom objects
class 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 False
d1 = 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
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 比较运算符