python 比较运算符_Python比较运算符

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 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 == b
True
>>> a != b
False
>>> c > a
True
>>> c < a
False
>>> c <= 20
True
>>> c >= 20
True
>>>
Python Comparison Operator Integer

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 Operator String

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 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 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

摘要 (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 比较运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值