Python Tutorial (44) - operator module

Time: Column:Python views:227

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:

OperationSyntaxFunction
Additiona + badd(a, b)
String Concatenationseq1 + seq2concat(seq1, seq2)
Containment Testobj in seqcontains(seq, obj)
Divisiona / btruediv(a, b)
Floor Divisiona // bfloordiv(a, b)
Bitwise ANDa & band_(a, b)
Bitwise XORa ^ bxor(a, b)
Bitwise NOT~ ainvert(a)
Bitwise OR`ab`
Exponentiationa ** bpow(a, b)
Identity Checka is bis_(a, b)
Not Identity Checka is not bis_not(a, b)
Index Assignmentobj[k] = vsetitem(obj, k, v)
Index Deletiondel obj[k]delitem(obj, k)
Index Valueobj[k]getitem(obj, k)
Left Shifta << blshift(a, b)
Modulusa % bmod(a, b)
Multiplicationa * bmul(a, b)
Matrix Multiplicationa @ bmatmul(a, b)
Negation (Arithmetic)-aneg(a)
Logical NOTnot anot_(a)
Positive+apos(a)
Right Shifta >> brshift(a, b)
Slice Assignmentseq[i:j] = valuessetitem(seq, slice(i, j), values)
Slice Deletiondel seq[i:j]delitem(seq, slice(i, j))
Slice Valueseq[i:j]getitem(seq, slice(i, j))
String Formattings % objmod(s, obj)
Subtractiona - bsub(a, b)
Truth Testobjtruth(obj)
Comparison (Less)a < blt(a, b)
Comparison (Less or Equal)a <= ble(a, b)
Equalitya == beq(a, b)
Not Equala != bne(a, b)
Comparison (Greater or Equal)a >= bge(a, b)
Comparison (Greater)a > bgt(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.