Python3 operator Module
In Python 2.x, the cmp() function was used to compare the size relationship of two lists, numbers, or strings, etc.
The cmp() function no longer exists in Python 3.X. If you need comparison functionality, you must import the operator module, which is suitable for any object and includes methods such as:
Methods contained in the operator module
operator
.
lt
(
a
,
b
)
operator
.
le
(
a
,
b
)
operator
.
eq
(
a
,
b
)
operator
.
ne
(
a
,
b
)
operator
.
ge
(
a
,
b
)
operator
.
gt
(
a
,
b
)
operator
.
lt
(
a
,
b
)
operator
.
le
(
a
,
b
)
operator
.
eq
(
a
,
b
)
operator
.
ne
(
a
,
b
)
operator
.
ge
(
a
,
b
)
operator
.
gt
(
a
,
b
)
operator.lt(a, b) The difference between a < b the same, operator.le(a, b) The difference between a <= b the same,operator.eq(a, b) The difference between a == b the same,operator.ne(a, b) The difference between a != b the same,operator.gt(a, b) The difference between a > b the same,operator.ge(a, b) The difference between a >= b .
Example
Import the operator module
import
operator
Digit
x
=
10
y
=
20
(
"x:"
,
x
,
", y:"
,
y
)
(
"operator.lt(x,y): "
,
operator
.
lt
(
x
,
y
)
)
(
"operator.gt(y,x): "
,
operator
.
gt
(
y
,
x
)
)
(
"operator.eq(x,x): "
,
operator
.
eq
(
x
,
x
)
)
(
"operator.ne(y,y): "
,
operator
.
ne
(
y
,
y
)
)
(
"operator.le(x,y): "
,
operator
.
le
(
x
,
y
)
)
(
"operator.ge(y,x): "
,
operator
.
ge
(
y
,
x
)
)
(
)
String
x
=
"Google"
y
=
"Runoob"
(
"x:"
,
x
,
", y:"
,
y
)
(
"operator.lt(x,y): "
,
operator
.
lt
(
x
,
y
)
)
(
"operator.gt(y,x): "
,
operator
.
gt
(
y
,
x
)
)
(
"operator.eq(x,x): "
,
operator
.
eq
(
x
,
x
)
)
(
"operator.ne(y,y): "
,
operator
.
ne
(
y
,
y
)
)
(
"operator.le(x,y): "
,
operator
.
le
(
x
,
y
)
)
(
"operator.ge(y,x): "
,
operator
.
ge
(
y
,
x
)
)
(
)
View the return value
(
"type((operator.lt(x,y)): "
,
type
(
operator
.
lt
(
x
,
y
)
)
)
The output of the code above is:
x: 10 , y: 20
operator.lt(x,y): True
operator.gt(y,x): True
operator.eq(x,x): True
operator.ne(y,y): False
operator.le(x,y): True
operator.ge(y,x): True
x: Google , y: Runoob
operator.lt(x,y): True
operator.gt(y,x): True
operator.eq(x,x): True
operator.ne(y,y): False
operator.le(x,y): True
operator.ge(y,x): True
Compare two lists:
Example
Import the operator module
import
operator
a
=
[
1
,
2
]
b
=
[
2
,
3
]
c
=
[
2
,
3
]
(
"operator.eq(a,b): "
,
operator
.
eq
(
a
,
b
)
)
(
"operator.eq(c,b): "
,
operator
.
eq
(
c
,
b
)
)
The output of the code above is:
operator.eq(a,b): False
operator.eq(c,b): True
Operator functions
The operator module provides a set of efficient functions corresponding to Python's built-in operators. For example,operator.add(x, y) is equivalent to the expression x+y .
The types of functions included are: object comparison, logical operations, mathematical operations, and sequence operations.
Object comparison functions apply to all objects; the function names are named according to their corresponding comparison operators.
Many function names are the same as special method names, but without the double underscores. For backward compatibility, many functions with double underscores are also retained; for clarity, it is recommended to use the functions without double underscores.
Example
Python example
add(), sub(), mul()
Import the operator module
import
operator
Initialize variables
a
=
4
b
=
3
Use add() to add two values
(
"add() result :"
,
end
=
""
)
;
(
operator
.
add
(
a
,
b
)
)
Use sub() to subtract two values
(
"sub() result :"
,
end
=
""
)
;
(
operator
.
sub
(
a
,
b
)
)
Use mul() to multiply two values
(
"mul() result :"
,
end
=
""
)
;
(
operator
.
mul
(
a
,
b
)
)
The output of the code above is:
add() 运算结果 :7
sub() 运算结果 :1
mul() 运算结果 :12
|
Operation |
Syntax |
function |
|---|---|---|
|
Addition |
|
|
|
String concatenation |
|
|
|
Containment test |
|
|
|
Division |
|
|
|
Division |
|
|
|
Bitwise AND |
|
|
|
Bitwise XOR |
|
|
|
Bitwise NOT |
|
|
|
Bitwise OR |
|
|
|
Exponentiation |
|
|
|
Identity |
|
|
|
Identity |
|
|
|
Index assignment |
|
|
|
Index deletion |
|
|
|
Index retrieval |
|
|
|
Left shift |
|
|
|
Modulo |
|
|
|
Multiplication |
|
|
|
Matrix multiplication |
|
|
|
Negation (arithmetic) |
|
|
|
Negation (logical) |
|
|
|
Positive |
|
|
|
Right shift |
|
|
|
Slice assignment |
|
|
|
Slice deletion |
|
|
|
Slice retrieval |
|
|
|
String formatting |
|
|
|
Subtraction |
|
|
|
Truth value testing |
|
|
|
Comparison |
|
|
|
Comparison |
|
|
|
Equality |
|
|
|
Not equal |
|
|
|
Comparison |
|
|
|
Comparison |
|
|