Skip to content

Python Operator Function

Unlocking Efficiency: Operator Functions in Python

The operator module in Python is a powerful tool that provides functions corresponding to standard operators. It simplifies complex operations, enhances code readability, and facilitates concise expression of common tasks. In this comprehensive guide, we’ll explore the operator module, its key functions, and how it contributes to efficient Python programming.

What is the operator module?

The operator module in Python provides functions corresponding to standard operators. It’s a powerful tool for simplifying complex operations, enhancing code readability, and facilitating concise expression of common tasks. The operator module is part of the Python standard library, so it doesn’t need to be installed separately. It’s available in all versions of Python.

Why use the operator module?

The operator module provides functions corresponding to standard operators. It simplifies complex operations, enhances code readability, and facilitates concise expression of common tasks. It’s a powerful tool for simplifying complex operations, enhancing code readability, and facilitating concise expression of common tasks. The operator module is part of the Python standard library, so it doesn’t need to be installed separately. It’s available in all versions of Python.

Key Functions Table

OperatorFunctionDescription
++operator.add(a, b)operator.add(a, b)Addition
--operator.sub(a, b)operator.sub(a, b)Subtraction
**operator.mul(a, b)operator.mul(a, b)Multiplication
//operator.truediv(a, b)operator.truediv(a, b)Division
%%operator.mod(a, b)operator.mod(a, b)Modulus
////operator.floordiv(a, b)operator.floordiv(a, b)Floor Division
****operator.pow(a, b)operator.pow(a, b)Exponentiation
&&operator.and_(a, b)operator.and_(a, b)Bitwise AND
||operator.or_(a, b)operator.or_(a, b)Bitwise OR
^^operator.xor(a, b)operator.xor(a, b)Bitwise XOR
~~operator.invert(a)operator.invert(a)Bitwise NOT
<<<<operator.lshift(a, b)operator.lshift(a, b)Bitwise left shift
>>>>operator.rshift(a, b)operator.rshift(a, b)Bitwise right shift
====operator.eq(a, b)operator.eq(a, b)Equal to
!=!=operator.ne(a, b)operator.ne(a, b)Not equal to
<<operator.lt(a, b)operator.lt(a, b)Less than
>>operator.gt(a, b)operator.gt(a, b)Greater than
<=<=operator.le(a, b)operator.le(a, b)Less than or equal to
>=>=operator.ge(a, b)operator.ge(a, b)Greater than or equal to
+=+=operator.iadd(a, b)operator.iadd(a, b)Addition
-=-=operator.isub(a, b)operator.isub(a, b)Subtraction
*=*=operator.imul(a, b)operator.imul(a, b)Multiplication
/=/=operator.itruediv(a, b)operator.itruediv(a, b)Division
%=%=operator.imod(a, b)operator.imod(a, b)Modulus
//=//=operator.ifloordiv(a, b)operator.ifloordiv(a, b)Floor Division
**=**=operator.ipow(a, b)operator.ipow(a, b)Exponentiation
&=&=operator.iand(a, b)operator.iand(a, b)Bitwise AND
|=|=operator.ior(a, b)operator.ior(a, b)Bitwise OR
^=^=operator.ixor(a, b)operator.ixor(a, b)Bitwise XOR
<<=<<=operator.ilshift(a, b)operator.ilshift(a, b)Bitwise left shift
>>=>>=operator.irshift(a, b)operator.irshift(a, b)Bitwise right shift
()()operator()operator()Call operator
[][]operator[]operator[]Index operator
()()operator[]operator[]Index assignment operator
deldeloperator[]operator[]Index deletion operator
len()len()operator.len()operator.len()Length operator
str()str()operator.str()operator.str()String conversion operator
repr()repr()operator.repr()operator.repr()Object representation operator
iter()iter()operator.iter()operator.iter()Iteration operator
next()next()operator.next()operator.next()Next iteration operator
reversed()reversed()operator.reversed()operator.reversed()Reverse iteration operator
cmp()cmp()operator.cmp()operator.cmp()Comparison operator
pos()pos()operator.pos()operator.pos()Unary plus
neg()neg()operator.neg()operator.neg()Unary minus
abs()abs()operator.abs()operator.abs()Absolute value
invert()invert()operator.invert()operator.invert()Bitwise NOT
complex()complex()operator.complex()operator.complex()Complex number conversion
int()int()operator.int()operator.int()Integer conversion
long()long()operator.long()operator.long()Long integer conversion
float()float()operator.float()operator.float()Float conversion
oct()oct()operator.oct()operator.oct()Octal conversion
hex()hex()operator.hex()operator.hex()Hexadecimal conversion
index()index()operator.index()operator.index()Conversion to an integer
trunc()trunc()operator.trunc()operator.trunc()Truncation operator
coerce()coerce()operator.coerce()operator.coerce()Coercion
enter()enter()operator.enter()operator.enter()Context management protocol
exit()exit()operator.exit()operator.exit()Context management protocol
hash()hash()operator.hash()operator.hash()Hashing operator
getattr()getattr()operator.getattr()operator.getattr()Attribute access
setattr()setattr()operator.setattr()operator.setattr()Attribute assignment
delattr()delattr()operator.delattr()operator.delattr()Attribute deletion
dir()dir()operator.dir()operator.dir()Attribute query
getattribute()getattribute()operator.getattribute()operator.getattribute()Attribute access
set()set()operator.set()operator.set()Descriptor access
delete()delete()operator.delete()operator.delete()Descriptor deletion
get()get()operator.get()operator.get()Descriptor access

Arithmetic Operators

++ & -- & ** & // & %% & //// & **** Operators

The arithmetic operators (++, --, **, //, %%, ////, ****) perform arithmetic operations on numeric operands. The following example demonstrates how to use the arithmetic operators in Python:

arithmetic_operators.py
# Arithmetic operators
import operator
x = 10
y = 5
print(operator.add(x, y))
print(operator.sub(x, y))
print(operator.mul(x, y))
print(operator.truediv(x, y))
print(operator.mod(x, y))
print(operator.floordiv(x, y))
print(operator.pow(x, y))
arithmetic_operators.py
# Arithmetic operators
import operator
x = 10
y = 5
print(operator.add(x, y))
print(operator.sub(x, y))
print(operator.mul(x, y))
print(operator.truediv(x, y))
print(operator.mod(x, y))
print(operator.floordiv(x, y))
print(operator.pow(x, y))

Output:

command
C:\Users\Your Name> python arithmetic_operators.py
15
5
50
2.0
0
2
100000
command
C:\Users\Your Name> python arithmetic_operators.py
15
5
50
2.0
0
2
100000

In the above example, we have used the arithmetic operators to perform arithmetic operations on numeric operands. The add()add() function adds two operands xx and yy. The sub()sub() function subtracts the second operand yy from the first operand xx. The mul()mul() function multiplies two operands xx and yy. The truediv()truediv() function divides the first operand xx by the second operand yy. The mod()mod() function returns the remainder of the division of the first operand xx by the second operand yy. The floordiv()floordiv() function returns the quotient of the division of the first operand xx by the second operand yy. The pow()pow() function returns the first operand xx raised to the power of the second operand yy.

Comparison Operators

==== & !=!= & << & >> & <=<= & >=>= Operators

The comparison operators (====, !=!=, <<, >>, <=<=, >=>=) compare two operands. They return TrueTrue if the comparison is true, otherwise they return FalseFalse. The following example demonstrates how to use the comparison operators in Python:

comparison_operators.py
# Comparison operators
import operator
x = 10
y = 5
print(operator.eq(x, y))
print(operator.ne(x, y))
print(operator.lt(x, y))
print(operator.gt(x, y))
print(operator.le(x, y))
print(operator.ge(x, y))
comparison_operators.py
# Comparison operators
import operator
x = 10
y = 5
print(operator.eq(x, y))
print(operator.ne(x, y))
print(operator.lt(x, y))
print(operator.gt(x, y))
print(operator.le(x, y))
print(operator.ge(x, y))

Output:

command
C:\Users\Your Name> python comparison_operators.py
False
True
False
True
False
True
command
C:\Users\Your Name> python comparison_operators.py
False
True
False
True
False
True

In the above example, we have used the comparison operators to compare two operands xx and yy. The eq()eq() function returns TrueTrue if the operands are equal, otherwise it returns FalseFalse. The ne()ne() function returns TrueTrue if the operands are not equal, otherwise it returns FalseFalse. The lt()lt() function returns TrueTrue if the first operand xx is less than the second operand yy, otherwise it returns FalseFalse. The gt()gt() function returns TrueTrue if the first operand xx is greater than the second operand yy, otherwise it returns FalseFalse. The le()le() function returns TrueTrue if the first operand xx is less than or equal to the second operand yy, otherwise it returns FalseFalse. The ge()ge() function returns TrueTrue if the first operand xx is greater than or equal to the second operand yy, otherwise it returns FalseFalse.

Bitwise Operators

&& & || & ^^ & ~~ & <<<< & >>>> Operators

The bitwise operators (&&, ||, ^^, ~~, <<<<, >>>>) perform bitwise operations on integer operands. The following example demonstrates how to use the bitwise operators in Python:

bitwise_operators.py
# Bitwise operators
import operator
x = 10
y = 5
print(operator.and_(x, y))
print(operator.or_(x, y))
print(operator.xor(x, y))
print(operator.invert(x))
print(operator.lshift(x, y))
print(operator.rshift(x, y))
bitwise_operators.py
# Bitwise operators
import operator
x = 10
y = 5
print(operator.and_(x, y))
print(operator.or_(x, y))
print(operator.xor(x, y))
print(operator.invert(x))
print(operator.lshift(x, y))
print(operator.rshift(x, y))

Output:

command
C:\Users\Your Name> python bitwise_operators.py
0
15
15
-11
320
0
command
C:\Users\Your Name> python bitwise_operators.py
0
15
15
-11
320
0

In the above example, we have used the bitwise operators to perform bitwise operations on integer operands. The and_()and_() function performs a bitwise AND operation on the operands xx and yy. The or_()or_() function performs a bitwise OR operation on the operands xx and yy. The xor()xor() function performs a bitwise XOR operation on the operands xx and yy. The invert()invert() function performs a bitwise NOT operation on the operand xx. The lshift()lshift() function performs a bitwise left shift operation on the operand xx by the number of bits specified by the operand yy. The rshift()rshift() function performs a bitwise right shift operation on the operand xx by the number of bits specified by the operand yy.

Assignment Operators

+=+= & -=-= & *=*= & /=/= & %=%= & //=//= & **=**= Operators

The assignment operators (+=+=, -=-=, *=*=, /=/=, %=%=, //=//=, **=**=) perform arithmetic operations on numeric operands and assign the result to the left operand. The following example demonstrates how to use the assignment operators in Python:

assignment_operators.py
# Assignment operators
import operator
x = 10
y = 5
operator.iadd(x, y)
print(x)
operator.isub(x, y)
print(x)
operator.imul(x, y)
print(x)
operator.itruediv(x, y)
print(x)
operator.imod(x, y)
print(x)
operator.ifloordiv(x, y)
print(x)
operator.ipow(x, y)
print(x)
assignment_operators.py
# Assignment operators
import operator
x = 10
y = 5
operator.iadd(x, y)
print(x)
operator.isub(x, y)
print(x)
operator.imul(x, y)
print(x)
operator.itruediv(x, y)
print(x)
operator.imod(x, y)
print(x)
operator.ifloordiv(x, y)
print(x)
operator.ipow(x, y)
print(x)

Output:

command
C:\Users\Your Name> python assignment_operators.py
15
10
50
10
0
2
100000
command
C:\Users\Your Name> python assignment_operators.py
15
10
50
10
0
2
100000

In the above example, we have used the assignment operators to perform arithmetic operations on numeric operands and assign the result to the left operand. The iadd()iadd() function adds two operands xx and yy and assigns the result to the variable xx. The isub()isub() function subtracts the second operand yy from the first operand xx and assigns the result to the variable xx. The imul()imul() function multiplies two operands xx and yy and assigns the result to the variable xx. The itruediv()itruediv() function divides the first operand xx by the second operand yy and assigns the result to the variable xx. The imod()imod() function returns the remainder of the division of the first operand xx by the second operand yy and assigns the result to the variable xx. The ifloordiv()ifloordiv() function returns the quotient of the division of the first operand xx by the second operand yy and assigns the result to the variable xx. The ipow()ipow() function returns the first operand xx raised to the power of the second operand yy and assigns the result to the variable xx.

Logical Operators

andand & oror & notnot Operators

The logical operators (andand, oror, notnot) perform logical operations on boolean operands. The following example demonstrates how to use the logical operators in Python:

logical_operators.py
# Logical operators
import operator
x = True
y = False
print(operator.and_(x, y))
print(operator.or_(x, y))
print(operator.not_(x))
logical_operators.py
# Logical operators
import operator
x = True
y = False
print(operator.and_(x, y))
print(operator.or_(x, y))
print(operator.not_(x))

Output:

command
C:\Users\Your Name> python logical_operators.py
False
True
False
command
C:\Users\Your Name> python logical_operators.py
False
True
False

In the above example, we have used the logical operators to perform logical operations on boolean operands. The and_()and_() function performs a logical AND operation on the operands xx and yy. The or_()or_() function performs a logical OR operation on the operands xx and yy. The not_()not_() function performs a logical NOT operation on the operand xx.

Identity Operators

isis & is notis not Operators

The identity operators (isis, is notis not) compare the memory addresses of two operands. They return TrueTrue if the memory addresses are equal, otherwise they return FalseFalse. The following example demonstrates how to use the identity operators in Python:

identity_operators.py
# Identity operators
import operator
x = 10
y = 5
print(operator.is_(x, y))
print(operator.is_not(x, y))
identity_operators.py
# Identity operators
import operator
x = 10
y = 5
print(operator.is_(x, y))
print(operator.is_not(x, y))

Output:

command
C:\Users\Your Name> python identity_operators.py
False
True
command
C:\Users\Your Name> python identity_operators.py
False
True

In the above example, we have used the identity operators to compare the memory addresses of two operands xx and yy. The is_()is_() function returns TrueTrue if the memory addresses of the operands are equal, otherwise it returns FalseFalse. The is_not()is_not() function returns TrueTrue if the memory addresses of the operands are not equal, otherwise it returns FalseFalse.

Conclusion

The operator module in Python provides a convenient and efficient way to work with standard operators as functions. Whether you’re performing basic arithmetic, comparisons, logical or bitwise operations, the module offers a streamlined approach to writing cleaner and more concise code.

As you continue to explore Python programming, consider incorporating the operator module into your toolkit for situations where its functions can enhance the readability and efficiency of your code. For more insights and practical examples, check out our tutorials on Python Central Hub!

Was this page helpful?

Let us know how we did