Python built-in type (3) -- Comparison, python built-in type
Python has eight comparison operators:<
,<=
,>
,>=
,==
,!=
,is
,is not
They have the same priority. Their meanings are as follows:
Operator |
Description |
< |
Less |
<= |
Less than or equal |
> |
Greater |
>= |
Greater than or equal |
== |
Equal |
!= |
Not equal |
is |
Whether the same object |
is not |
Is it not the same object? |
Comparison operators have the following features:
Two different objects of the same type can be compared.
# Integer >>> 2> 1 True >>> 2> = 1 True >>> 2 <1 False >>> 2 <= 1 False >>> 2 = 1 False >>> 2! = 1 True >>> 2 is 1 False >>>> 2 is not 1 True # string >>> 'A'> 'B' False >>>> 'A'> =' B 'False >>>> 'A' <' B 'True >>>> 'A' <=' B 'True >>> 'A' =' B 'False>> 'A '! = 'B' True >>> a is 'B' False >>>> a is not 'B' True # sequence type >>>( 1, 2)> (2, 3) false >>> () >=() False >>>() <() True >>> () <=) true> () = () False >>> )! = (2, 3) True >>> (1, 2) is (2, 3) False >>> (1, 2) is not (2, 3) True
The size comparison operator can only compare two objects of the same type. A syntax error is returned when comparing objects of different types.
# Compare the sizes of objects of the same type> 1> = 0 True> 'A' <'B' True>) true # objects of different types cannot be compared. >>> 1> = '1' Traceback (most recent call last): File "<pyshell #15> ", line 1, in <module> 1> = '1' TypeError: unorderable types: int ()> str () >>>( 1, 2) <= [1, 2] Traceback (most recent call last): File "<pyshell #16>", line 1, in <module> (1, 2) <= [1, 2] TypeError: unorderable types: tuple ()> = list ()
The same value type. The size of an integer object can be compared with that of a floating-point object. Objects of the plural type cannot be compared with integer or floating-point objects.
# The size of an integer object and a floating point object can be compared. >>>> 2.1> 2 True >>>-1.1 <0 True # objects of the complex type and integer or floating point type cannot be compared.>> 1 + 1j> 1 Traceback (most recent call last): File "<pyshell #22>", line 1, in <module> 1 + 1j> 1 TypeError: unorderable types: complex ()> int ()
Objects of different types can be equal.
==
And
!=
Comparison, results are not equal
>>> 1 == "1"False>>> 1 == (1)False>>> 1 == [1]False
When the objects of the integer and floating-point types are equal, if the values of the two objects are the same, the results are equal.
>>> 1 == 1.0True
The two instance objects of the custom class are not equal and cannot be compared between them.
>>> class Man(): def __init__(self,name): self.name = name>>> a = Man('xiaoming')>>> b = Man('xiaoming')>>> a == bFalse>>> a != bTrue>>> a > bTraceback (most recent call last): File "<pyshell#10>", line 1, in <module> a > bTypeError: unorderable types: Man() > Man()
If the custom class is implemented
__eq__()
Method, you can perform equal comparison. The result of equal comparison is
__eq__()
The result returned by the method execution. If the result is not equal to the comparison, the User-Defined result is prioritized.
__ne__()
Method. If there is an implementation, the comparison result is not equal
__ne__()
The result returned by the method execution. Otherwise, the result is equal to the inverse value of the comparison result.
# Implement _ eq __>>> class Man (): def _ init _ (self, name): self. name = name def _ eq _ (self, other): return self. name = other. name >>> a = Man ('xiaoming') >>> B = Man ('xiaoming') >>> a = B # implement the _ eq _ method True >>>! = B # implements the _ eq _ method, returns False if the comparison result is equal. # Both _ eq _ and _ ne __>>> class Man (): def _ init _ (self, name): self. name = name def _ eq _ (self, other): return self. name = other. name def _ ne _ (self, other): return False >>> a = Man ('ming') >>> B = Man ('ming') >>> a = bTrue >>>! = BFalse # The size ratio is not allowed. >>> a> bTraceback (most recent call last): File "<pyshell #7>", line 1, in <module> a> bTypeError: unorderable types: Man ()> Man ()
Similarly, if the custom class implements
__lt __()
,
__le __()
,
__gt __()
And
__ge __()
You can compare the sizes of different instances of the custom class.
__lt __()
And
__le __()
The two methods are sufficient, so that the instance object can be compared and judged.
>>> class Man(): def __init__(self,name): self.name = name def __le__(self,other): return self.name <= other.name def __lt__(self,other): return self.name < other.name>>> a = Man('a')>>> b = Man('b')>>> a > bFalse>>> a >= bFalse>>>
is
And
is not
It is used to compare whether two objects contain the same value or point to the same object. They cannot be customized. They can also be applied to any two objects and never cause exceptions.
# Objects of the same type >>> 1 is 1 True >>> 1 is 1.0 False # objects of different types >>> 1 is '1' False >>> 1 is not' 1 'true # different instance objects of the class >>> a = object () >>> B = object () >>> a is bFalse
in
And
not in
Operator, used to determine whether an object is in a sequence, and can only be used for sequence types.
>>> '1' in '12'True>>> '1' in (1,2,3)False