operator
module. These functions can serve as substitutes for the traditional operators, allowing for enhanced functionality and flexibility in your Python code.In this article, we will explore the operator functions in Python, how to use them, and provide practical examples to illustrate their applications.operator
module provides functions that correspond to the built-in operators. For example, the mul()
function in the operator module is equivalent to the *
multiplication operator. These operator functions allow you to perform mathematical, logical, and relational operations just like their symbolic counterparts. However, they can be useful in situations where you prefer to use functions instead of operators or when working with higher-order functions like map()
or filter()
.Before using the operator functions, you’ll need to import the operator
module:Operator Function | Description | Example |
---|---|---|
add(a, b) | Adds two operands | add(3, 5) → 8 |
sub(a, b) | Subtracts the second operand from the first | sub(8, 3) → 5 |
mul(a, b) | Multiplies two operands | mul(3, 5) → 15 |
truediv(a, b) | Divides the first operand by the second (true division) | truediv(10, 2) → 5.0 |
floordiv(a, b) | Performs floor division | floordiv(10, 3) → 3 |
mod(a, b) | Returns the remainder of division | mod(10, 3) → 1 |
pow(a, b) | Returns a raised to the power of b | pow(2, 3) → 8 |
Operator Function | Description | Example |
---|---|---|
lt(a, b) | Checks if a is less than b | lt(3, 5) → True |
le(a, b) | Checks if a is less than or equal to b | le(3, 5) → True |
gt(a, b) | Checks if a is greater than b | gt(5, 3) → True |
ge(a, b) | Checks if a is greater than or equal to b | ge(5, 5) → True |
eq(a, b) | Checks if a is equal to b | eq(5, 5) → True |
ne(a, b) | Checks if a is not equal to b | ne(5, 3) → True |
operator
module.Operator Function | Description | Example |
---|---|---|
and_(a, b) | Performs bitwise AND operation | and_(3, 4) → 0 |
or_(a, b) | Performs bitwise OR operation | or_(3, 4) → 7 |
xor(a, b) | Performs bitwise XOR operation | xor(3, 4) → 7 |
invert(a) | Performs bitwise NOT operation | invert(3) → -4 |
setitem(object, position, value)
: Updates a specific value at a given position in a list.delitem(object, position)
: Deletes an item from a list at a given position.getitem(object, position)
: Retrieves a value from a specified position.concat(object1, object2)
: Concatenates two objects (e.g., lists or strings).contains(object, value)
: Checks if a value is present in an object.operator
module offers a range of operator functions that can make your code more versatile and clean, especially when working with higher-order functions. By using functions like add()
, sub()
, mul()
, and_()
, and others, you can perform arithmetic, bitwise, and relational operations just as easily as with their operator counterparts. These functions also provide additional flexibility, making them ideal for dynamic operations on data structures.Click here to know more our program!