# Python import operator

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.

## 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: "))

# 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.