Python比较运算符
时间:2020-02-23 14:42:32 来源:igfitidea点击:
Python比较运算符用于比较两个对象。
返回的输出是布尔值-True或者False。
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 |
Python比较运算符示例
让我们看一个简单的示例,将比较运算符与原始数据类型(例如整数)一起使用。
>>> 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比较运算符
该字符串是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比较运算符–字符串
那么这是否意味着比较运算符将可以与任何python对象一起使用?
让我们通过创建一个自定义类来进行检查。
>>> 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: '>' 不支持 between instances of 'Data' and 'Data' >>> d1 < d2 Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> d1 < d2 TypeError: '<' 不支持 between instances of 'Data' and 'Data' >>> d1 <= d2 Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> d1 <= d2 TypeError: '<=' 不支持 between instances of 'Data' and 'Data' >>> d1 >= d2 Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> d1 >= d2 TypeError: '>=' 不支持 between instances of 'Data' and 'Data' >>> >>>
Python比较运算符重载错误
为什么等于和不等于运算符起作用,而其他运算符却不起作用?
这是因为"对象"是Python中每个类的基础。
对象提供了用于等于和不等于运算符的函数的实现。
比较运算符的功能
这是比较运算符使用的功能列表。
因此,如果希望比较运算符与自定义对象一起使用,则需要为其提供一个实现。
| Comparison Operator | Function |
|---|---|
| == | __eq__(self, other) |
| != | __ne__(self, other) |
| > | __gt__(self, other) |
| < | __lt__(self, other) |
| >= | __ge__(self, other) |
| <= | __le__(self, other) |
Python比较运算符重载
让我们看一个在自定义对象中重载比较运算符的示例。
# 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}')
输出:
== 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

