Skip to content

Python Operator Function

Unlocking Efficiency: Operator Functions in Python

Section titled “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.

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.

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.

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

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))

Output:

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() function adds two operands x and y. The sub() function subtracts the second operand y from the first operand x. The mul() function multiplies two operands x and y. The truediv() function divides the first operand x by the second operand y. The mod() function returns the remainder of the division of the first operand x by the second operand y. The floordiv() function returns the quotient of the division of the first operand x by the second operand y. The pow() function returns the first operand x raised to the power of the second operand y.

The comparison operators (==, !=, <, >, <=, >=) compare two operands. They return True if the comparison is true, otherwise they return False. 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))

Output:

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 x and y. The eq() function returns True if the operands are equal, otherwise it returns False. The ne() function returns True if the operands are not equal, otherwise it returns False. The lt() function returns True if the first operand x is less than the second operand y, otherwise it returns False. The gt() function returns True if the first operand x is greater than the second operand y, otherwise it returns False. The le() function returns True if the first operand x is less than or equal to the second operand y, otherwise it returns False. The ge() function returns True if the first operand x is greater than or equal to the second operand y, otherwise it returns False.

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))

Output:

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_() function performs a bitwise AND operation on the operands x and y. The or_() function performs a bitwise OR operation on the operands x and y. The xor() function performs a bitwise XOR operation on the operands x and y. The invert() function performs a bitwise NOT operation on the operand x. The lshift() function performs a bitwise left shift operation on the operand x by the number of bits specified by the operand y. The rshift() function performs a bitwise right shift operation on the operand x by the number of bits specified by the operand y.

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

Section titled “+= & -= & *= & /= & %= & //= & **= 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)

Output:

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() function adds two operands x and y and assigns the result to the variable x. The isub() function subtracts the second operand y from the first operand x and assigns the result to the variable x. The imul() function multiplies two operands x and y and assigns the result to the variable x. The itruediv() function divides the first operand x by the second operand y and assigns the result to the variable x. The imod() function returns the remainder of the division of the first operand x by the second operand y and assigns the result to the variable x. The ifloordiv() function returns the quotient of the division of the first operand x by the second operand y and assigns the result to the variable x. The ipow() function returns the first operand x raised to the power of the second operand y and assigns the result to the variable x.

The logical operators (and, or, not) 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))

Output:

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_() function performs a logical AND operation on the operands x and y. The or_() function performs a logical OR operation on the operands x and y. The not_() function performs a logical NOT operation on the operand x.

The identity operators (is, is not) compare the memory addresses of two operands. They return True if the memory addresses are equal, otherwise they return False. 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))

Output:

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 x and y. The is_() function returns True if the memory addresses of the operands are equal, otherwise it returns False. The is_not() function returns True if the memory addresses of the operands are not equal, otherwise it returns False.

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!


diagram the operator module gives operators a name you can pass mermaid
An operator is syntax, so it cannot be handed to sorted or reduce directly. The operator module provides the same behaviour as ordinary functions, plus three factory helpers that build the small accessor functions people otherwise write as lambdas.
sketch itemgetter against a lambda, doing the same job p5.js
Both extract the same field for a sort key. itemgetter is built in C and skips a Python-level call per element, which measured about 1.65 times faster on a small list. The larger benefit is that itemgetter with several indices returns a tuple, which is exactly what a multi-key sort needs.
pch.quizTag pch.quizDefaultTitle
  1. Why can you not pass `+` directly to `functools.reduce`?

    pch.quizShowAnswer

    B — `+` is syntax, not a value — `operator.add` is the function form — Operators are part of the grammar. The `operator` module exposes the same behaviour as ordinary callables so they can be passed around.

  2. What does `operator.itemgetter(1, 0)(('bob', 3))` return?

    pch.quizShowAnswer

    C — `(3, 'bob')` — Several indices give a tuple in the order you asked for. Since tuples compare element by element, that is a complete multi-key sort key in one call.

  3. Measured on a small list, `key=itemgetter(1)` beat `key=lambda r: r[1]` by about how much?

    pch.quizShowAnswer

    B — About 1.65x — 1812 ns against 1098 ns. It is implemented in C and avoids a Python-level call per element — a modest win, and not the main reason to use it.

  4. Which factory would you use to sort objects by their `age` attribute?

    pch.quizShowAnswer

    B — `attrgetter('age')` — `itemgetter` does `obj[...]`, `attrgetter` does `obj.attr`, and `methodcaller` calls a method. `attrgetter` also supports dotted paths, such as `attrgetter('address.city')`.

Exercise 1 – operator.add and operator.mul

Section titled “Exercise 1 – operator.add and operator.mul”

Exercise 2 – operator.lt and operator.eq

Section titled “Exercise 2 – operator.lt and operator.eq”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading