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
Operator | Function | Description |
---|---|---|
+ + | 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 |
del del | operator[] 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
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
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:
C:\Users\Your Name> python arithmetic_operators.py
15
5
50
2.0
0
2
100000
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 x
x
and y
y
. The sub()
sub()
function subtracts the second operand y
y
from the first operand x
x
. The mul()
mul()
function multiplies two operands x
x
and y
y
. The truediv()
truediv()
function divides the first operand x
x
by the second operand y
y
. The mod()
mod()
function returns the remainder of the division of the first operand x
x
by the second operand y
y
. The floordiv()
floordiv()
function returns the quotient of the division of the first operand x
x
by the second operand y
y
. The pow()
pow()
function returns the first operand x
x
raised to the power of the second operand y
y
.
Comparison Operators
==
==
& !=
!=
& <
<
& >
>
& <=
<=
& >=
>=
Operators
The comparison operators (==
==
, !=
!=
, <
<
, >
>
, <=
<=
, >=
>=
) compare two operands. They return True
True
if the comparison is true, otherwise they return False
False
. The following example demonstrates how to use the comparison operators in Python:
# 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
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:
C:\Users\Your Name> python comparison_operators.py
False
True
False
True
False
True
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
x
and y
y
. The eq()
eq()
function returns True
True
if the operands are equal, otherwise it returns False
False
. The ne()
ne()
function returns True
True
if the operands are not equal, otherwise it returns False
False
. The lt()
lt()
function returns True
True
if the first operand x
x
is less than the second operand y
y
, otherwise it returns False
False
. The gt()
gt()
function returns True
True
if the first operand x
x
is greater than the second operand y
y
, otherwise it returns False
False
. The le()
le()
function returns True
True
if the first operand x
x
is less than or equal to the second operand y
y
, otherwise it returns False
False
. The ge()
ge()
function returns True
True
if the first operand x
x
is greater than or equal to the second operand y
y
, otherwise it returns False
False
.
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
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
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:
C:\Users\Your Name> python bitwise_operators.py
0
15
15
-11
320
0
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 x
x
and y
y
. The or_()
or_()
function performs a bitwise OR operation on the operands x
x
and y
y
. The xor()
xor()
function performs a bitwise XOR operation on the operands x
x
and y
y
. The invert()
invert()
function performs a bitwise NOT operation on the operand x
x
. The lshift()
lshift()
function performs a bitwise left shift operation on the operand x
x
by the number of bits specified by the operand y
y
. The rshift()
rshift()
function performs a bitwise right shift operation on the operand x
x
by the number of bits specified by the operand y
y
.
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
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
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:
C:\Users\Your Name> python assignment_operators.py
15
10
50
10
0
2
100000
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 x
x
and y
y
and assigns the result to the variable x
x
. The isub()
isub()
function subtracts the second operand y
y
from the first operand x
x
and assigns the result to the variable x
x
. The imul()
imul()
function multiplies two operands x
x
and y
y
and assigns the result to the variable x
x
. The itruediv()
itruediv()
function divides the first operand x
x
by the second operand y
y
and assigns the result to the variable x
x
. The imod()
imod()
function returns the remainder of the division of the first operand x
x
by the second operand y
y
and assigns the result to the variable x
x
. The ifloordiv()
ifloordiv()
function returns the quotient of the division of the first operand x
x
by the second operand y
y
and assigns the result to the variable x
x
. The ipow()
ipow()
function returns the first operand x
x
raised to the power of the second operand y
y
and assigns the result to the variable x
x
.
Logical Operators
and
and
& or
or
& not
not
Operators
The logical operators (and
and
, or
or
, not
not
) perform logical operations on boolean operands. The following example demonstrates how to use the logical operators in Python:
# Logical operators
import operator
x = True
y = False
print(operator.and_(x, y))
print(operator.or_(x, y))
print(operator.not_(x))
# Logical operators
import operator
x = True
y = False
print(operator.and_(x, y))
print(operator.or_(x, y))
print(operator.not_(x))
Output:
C:\Users\Your Name> python logical_operators.py
False
True
False
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 x
x
and y
y
. The or_()
or_()
function performs a logical OR operation on the operands x
x
and y
y
. The not_()
not_()
function performs a logical NOT operation on the operand x
x
.
Identity Operators
is
is
& is not
is not
Operators
The identity operators (is
is
, is not
is not
) compare the memory addresses of two operands. They return True
True
if the memory addresses are equal, otherwise they return False
False
. The following example demonstrates how to use the identity operators in Python:
# Identity operators
import operator
x = 10
y = 5
print(operator.is_(x, y))
print(operator.is_not(x, y))
# Identity operators
import operator
x = 10
y = 5
print(operator.is_(x, y))
print(operator.is_not(x, y))
Output:
C:\Users\Your Name> python identity_operators.py
False
True
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
x
and y
y
. The is_()
is_()
function returns True
True
if the memory addresses of the operands are equal, otherwise it returns False
False
. The is_not()
is_not()
function returns True
True
if the memory addresses of the operands are not equal, otherwise it returns False
False
.
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