Python3 operator Module
In Python 2.x, the cmp() function was used to compare the size relationship of two lists, numbers, or strings.
In Python 3.X, the cmp() function no longer exists. If you need to implement comparison functionality, you need to import the operator module, suitable for any object, with the following methods:
Methods Included in the operator Module
Section titled “Methods Included in the operator Module”operator.lt(a, b) is the same as a < b, operator.le(a, b) is the same as a <= b, operator.eq(a, b) is the same as a == b, operator.ne(a, b) is the same as a != b, operator.gt(a, b) is the same as a > b, operator.ge(a, b) is the same as a >= b.
Example
Section titled “Example”The output of the above code is:
Comparing two lists:
Example
Section titled “Example”The output of the above code is:
Operator Functions
Section titled “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 the same as the expression x+y.
The categories of functions include: object comparison operations, logical operations, mathematical operations, and sequence operations.
Object comparison functions are suitable for all objects, and the function names are named according to their corresponding comparison operators.
Many function names are the same as special method names, just without the double underscores. For backward compatibility, many functions with double underscores are also retained. For clarity, it is recommended to use functions without double underscores.
Example
Section titled “Example”The output of the above code is:
| Operation | Syntax | Function |
|---|---|---|
| Addition | a + b |
add(a, b) |
| String Concatenation | seq1 + seq2 |
concat(seq1, seq2) |
| Containment Test | obj in seq |
contains(seq, obj) |
| Division | a / b |
truediv(a, b) |
| Floor Division | a // b |
floordiv(a, b) |
| Bitwise AND | a & b |
and_(a, b) |
| Bitwise XOR | a ^ b |
xor(a, b) |
| Bitwise NOT | ~ a |
invert(a) |
| Bitwise OR | `a | b` |
| Exponentiation | a ** b |
pow(a, b) |
| Identity | a is b |
is_(a, b) |
| Identity | a is not b |
is_not(a, b) |
| Index Assignment | obj[k] = v |
setitem(obj, k, v) |
| Index Deletion | del obj[k] |
delitem(obj, k) |
| Index Retrieval | obj[k] |
getitem(obj, k) |
| Left Shift | a << b |
lshift(a, b) |
| Modulo | a % b |
mod(a, b) |
| Multiplication | a * b |
mul(a, b) |
| Matrix Multiplication | a @ b |
matmul(a, b) |
| Negation (Arithmetic) | - a |
neg(a) |
| Negation (Logical) | not a |
not_(a) |
| Positive | + a |
pos(a) |
| Right Shift | a >> b |
rshift(a, b) |
| Slice Assignment | seq[i:j] = values |
setitem(seq, slice(i, j), values) |
| Slice Deletion | del seq[i:j] |
delitem(seq, slice(i, j)) |
| Slice Retrieval | seq[i:j] |
getitem(seq, slice(i, j)) |
| String Formatting | s % obj |
mod(s, obj) |
| Subtraction | a - b |
sub(a, b) |
| Truth Test | obj |
truth(obj) |
| Comparison | a < b |
lt(a, b) |
| Comparison | a <= b |
le(a, b) |
| Equality | a == b |
eq(a, b) |
| Inequality | a != b |
ne(a, b) |
| Comparison | a >= b |
ge(a, b) |
| Comparison | a > b |
gt(a, b) |