The Python operator module is one of the inbuilt modules in Python. By import operator, you can perform various operations and operate two input numbers in a Python program.
The functions provided by the operator module can use various mathematical, relational, logical, and bitwise operations on two input numbers
Functions supplied by the operator module.
Method | Signature | Behaves like |
---|---|---|
abs | abs( a ) | abs( a ) |
add | add( a , b ) | a + b |
and_ | and_( a , b ) | a & b |
concat | concat( a , b ) | a + b |
contains | contains( a , b ) | b in a |
countOf | countOf( a , b ) | a .count( b ) |
delitem | delitem( a , b ) | del a [ b ] |
delslice | delslice( a , b , c ) | del a [ b : c ] |
div | div( a , b ) | a / b |
eq | eq( a , b ) | a == b |
floordiv | floordiv( a , b ) | a // b |
ge | ge( a , b ) | a >= b |
getitem | getitem( a , b ) | a [ b ] |
getslice | getslice( a , b , c ) | a [ b : c ] |
gt | gt( a , b ) | a > b |
indexOf | indexOf( a , b ) | a .index( b ) |
invert, inv | invert( a ) , inv ( a ) | ~ a |
is | is( a , b ) | a is b |
is_not | is_not( a , b ) | a is not b |
le | le( a , b ) | a <= b |
lshift | lshift( a , b ) | a << b |
lt | lt( a , b ) | a < b |
mod | mod( a , b ) | a % b |
mul | mul( a , b ) | a * b |
ne | ne( a , b ) | a != b |
neg | neg( a ) | - a |
not_ | not_( a ) | not a |
or_ | or_( a , b ) | a | b |
pos | pos( a ) | + a |
repeat | repeat( a , b ) | a * b |
rshift | rshift( a , b ) | a >> b |
setitem | setitem( a , b , c ) | a [ b ]= c |
setslice | setslice( a , b , c , d ) | a [ b : c ]= d |
sub | sub( a , b ) | a - b |
truediv | truediv( a , b ) | a / b # "true" div -> no truncation |
truth | truth( a ) | not not a , bool( a ) |
xor_ | xor( a , b ) | a ^ b |
Source: https://www.oreilly.com/library/view/python-in-a/0596100469/ch15s02.html
Python import operator
Simple example code.
import operator
# input numbers from user
x = int(input("Enter first integer number: "))
y = int(input("Enter second integer number: "))
# Adding both input numbers
add = operator.add(x, y)
print("Addition: ", add)
# Subtracting both input numbers
sub = operator.sub(x, y)
print("Subtraction: ", sub)
# Multiply both input numbers
mul = operator.mul(x, y)
print("Multiplication: ", mul)
# Divide both input numbers
truediv = operator.truediv(x, y)
print("True division: ", truediv)
# floor division
fldiv = operator.floordiv(x, y)
print("Floor division: ", fldiv)
# modulus operation
mod = operator.mod(x, y)
print("Modulus: ", mod)
# power value operation
pow = operator.pow(x, y)
print("Exponentiation: ", pow)
Output:
Here’s a simple example:
import operator
# Sample data
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
# Sort the list of dictionaries based on the 'age' key
sorted_data = sorted(data, key=operator.itemgetter('age'))
# Print the sorted data
print(sorted_data)
In this example, operator.itemgetter('age')
is used as the key function for sorting the list of dictionaries based on the ‘age’ key.
Comment if you have any doubts or suggestions on this Python operator topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.