In Python 2.x versions, the cmp()
function was used to compare two lists, numbers, strings, and other data types. However, in Python 3.x, the cmp()
function has been removed. To achieve comparison functionality, you need to import the operator
module, which works for any object type. This module contains the following methods:
Methods in the operator
Module
operator.lt(a, b): Equivalent to
a < b
operator.le(a, b): Equivalent to
a <= b
operator.eq(a, b): Equivalent to
a == b
operator.ne(a, b): Equivalent to
a != b
operator.ge(a, b): Equivalent to
a >= b
operator.gt(a, b): Equivalent to
a > b
These functions can be used to compare various data types such as numbers, strings, and even lists.
Example
# Importing the operator module import operator # Numeric comparison x = 10 y = 20 print("x:", x, ", y:", y) print("operator.lt(x,y): ", operator.lt(x,y)) print("operator.gt(y,x): ", operator.gt(y,x)) print("operator.eq(x,x): ", operator.eq(x,x)) print("operator.ne(y,y): ", operator.ne(y,y)) print("operator.le(x,y): ", operator.le(x,y)) print("operator.ge(y,x): ", operator.ge(y,x)) print() # String comparison x = "Google" y = "PMeve" print("x:", x, ", y:", y) print("operator.lt(x,y): ", operator.lt(x,y)) print("operator.gt(y,x): ", operator.gt(y,x)) print("operator.eq(x,x): ", operator.eq(x,x)) print("operator.ne(y,y): ", operator.ne(y,y)) print("operator.le(x,y): ", operator.le(x,y)) print("operator.ge(y,x): ", operator.ge(y,x)) print() # Checking return value type print("type(operator.lt(x,y)): ", type(operator.lt(x,y)))
The output of this code will be:
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: PMeve 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
Comparing Two Lists
# Importing the operator module import operator a = [1, 2] b = [2, 3] c = [2, 3] print("operator.eq(a,b): ", operator.eq(a,b)) print("operator.eq(c,b): ", operator.eq(c,b))
Output:
operator.eq(a,b): False operator.eq(c,b): True
Operator Functions for Mathematical Operations
The operator
module also provides a set of high-performance functions that correspond to Python's built-in operators. For example, operator.add(x, y)
is equivalent to the expression x + y
.
These functions cover comparison, logical, mathematical, and sequence operations.
Example:
# Importing the operator module import operator # Initializing variables a = 4 b = 3 # Using add() for addition print("add() result:", operator.add(a, b)) # Using sub() for subtraction print("sub() result:", operator.sub(a, b)) # Using mul() for multiplication print("mul() result:", operator.mul(a, b))
Output:
add() result: 7 sub() result: 1 mul() result: 12
Operations and Corresponding Functions
Here’s a quick reference guide to common Python operations and their corresponding operator
module functions:
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 Check | a is b | is_(a, b) |
Not Identity Check | 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 Value | obj[k] | getitem(obj, k) |
Left Shift | a << b | lshift(a, b) |
Modulus | a % b | mod(a, b) |
Multiplication | a * b | mul(a, b) |
Matrix Multiplication | a @ b | matmul(a, b) |
Negation (Arithmetic) | -a | neg(a) |
Logical NOT | 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 Value | 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 (Less) | a < b | lt(a, b) |
Comparison (Less or Equal) | a <= b | le(a, b) |
Equality | a == b | eq(a, b) |
Not Equal | a != b | ne(a, b) |
Comparison (Greater or Equal) | a >= b | ge(a, b) |
Comparison (Greater) | a > b | gt(a, b) |
These operator functions provide flexibility and efficiency when handling comparisons, mathematical operations, and more. They can be used on numbers, strings, lists, and other data types to perform a wide range of operations.