codescracker
python

Python operator Module



« Python Tutorial Python Examples »

To perform operations using methods instead of using operators in a Python program, we've operator module, that provides a lot of methods to perform the operations. This article deals with these methods available in Python's operator module:

The details like brief description and example of all methods are given below, one by one. Let's have a look at all these to get every things about operator module's methods. To use any of the above methods, we must import operator module. And all the method must be used along with operator like shown in the syntax given below:

import operator

operator.method_name()

add() Method

The add() method of operator module works same as + operator. That is, this method is used for addition. But the difference is, to use add() method, we can proceed like:

res = operator.add(a, b)

The above statement indicates that the addition result of a and b gets initialized to res variable. Don't forgot to import operator module before using its methods say add(). To import operator module, use following statement:

import operator

The statement, res = operator.add(a, b) is equivalent to:

res = a + b

Here is an example program that implements add() method of operator module:

import operator

a, b = 10, 20
res = operator.add(a, b)
print(res)

Here is the snapshot taken, of the output produced by above program:

python operator module add method

sub() Method

Just like add() method, sub() method is used for subtraction. Here is an example program uses sub() method of operator module:

import operator

a, b = 6, 4
res = operator.sub(a, b)
print(res)

The output will be 2.

mul() Method

The mul() method works same as *, used for multiplication. Here is an example program uses mul() method:

import operator

a, b = 6, 4
res = operator.mul(a, b)
print(res)

The output produced by this program is 24.

truediv() Method

The truediv() method works same as /, used for division. The example given below uses truediv() method:

import operator

a, b = 6, 4
res = operator.truediv(a, b)
print(res)

The output produced by this program will be 1.5. That is, the true/exact division result.

floordiv() Method

The floordiv() method of operator module works same as //, that is also used for division. But the result comes out after dividing two numbers using floordiv() will not be the exact division result. Rather it will be the nearest integer value which is less than or equal to the result. Here is an example program of floordiv():

import operator

a, b = 6, 4
res = operator.floordiv(a, b)
print(res)

This time, the output produced by above program is 1.

mod() Method

The mod() method works same as %, used to find modulo. The example program that uses mod() method of operator module, is given below:

import operator

a, b = 6, 4
res = operator.mod(a, b)
print(res)

The output produced by above program will be 2, that shows the remainder value while dividing 6 by 4.

pow() Method

The pow() method of operator module works same as **, used for exponentiation. That is a ** b gets treated as ab. Here is an example program uses pow() method:

import operator

a, b = 2, 5
res = operator.pow(a, b)
print(res)

32 is the output produced by this program. Since operator.pow(a, b) evaluates to be ab, and again this evaluates to be 25, that gives 32.

eq() Method

The eq() method of operator module works same as ==, used to check for equality. That is, whether any two variable, value or operand is equal or not. Here is an example uses eq() method:

import operator

a, b = 6, 5
res = operator.eq(a, b)
print(res)

The output produced by above program is False, since the value of a is not equal to the value of b. Here is one famous use of equality check:

import operator

print("Enter first Input: ", end="")
a = input()
print("Enter second Input: ", end="")
b = input()
if operator.eq(a, b):
    print("\nBoth are equal")
else:
    print("\nBoth are not equal")

Here is its sample run with user input codescracker as both first and second input:

python operator module eq method

And here is another sample run with user input 8 as both first and second input:

python operator module eq method example

That is, the expression operator.eq(8, 8) evaluates to be True, therefore the program program flow goes inside the if's block and evaluates the print statement, that prints Both are equal.

ne() Method

The ne() method works same as !=, used to check whether any two variable or directly value are not equal to each other or not. That is, using this method, if both variable is not equal to each other, then it returns True, otherwise False gets returned. Here is an example program, uses ne() (not equal) method:

import operator

a, b = 6, 5
res = operator.ne(a, b)
print(res)

This program produces True as output. Since 6 is not equal to 5.

gt() Method

The gt() method of operator module works same as >, used to check whether the first argument passed is greater than the second argument passed or not. If first one will be greater than second, then True gets returned, otherwise False gets returned. Here is an example program uses gt() method:

import operator

a, b = 6, 5
res = operator.gt(a, b)
print(res)

The output will be True, since a's value is greater than b's value. Here is another example that shows the famous use of gt() method:

import operator

print("Enter first Number: ", end="")
num_one = int(input())
print("Enter second Number: ", end="")
num_two = int(input())
if operator.gt(num_one, num_two):
    print(num_one, "is greater than", num_two)
else:
    print(num_one, "is less than", num_two)

Here is its sample run with user input 20 as first and 10 as second number:

python operator module gt method

lt() Method

The lt() method works same as <, used to check whether the value of first argument is less than the value of second argument (passed to the method) or not. Here is an example uses lt() method of operator module:

import operator

a, b = 6, 5
res = operator.lt(a, b)
print(res)

Produces False as output since a (6) is not less than b (5).

ge() Method

The ge() method of operator module works same as >= operator, used to check whether the first argument is greater than or equal to second argument passed to the method or not. It returns either True or False. Here is an example uses the ge() (greater/equal) method:

import operator

a, b = 6, 5
res = operator.ge(a, b)
print(res)

Produces True as output, since a (6) is greater than b (5).

le() Method

The le() works same as <= operator, used to check whether the first argument passed is less than or equal to the second argument passed to method or not. It returns either True or False. The example program that uses le() method is given below:

import operator

a, b = 6, 5
res = operator.le(a, b)
print(res)

Produces False as output.

and_() Method

The and_() method of operator module works same as Bitwise & operator. You can refer to Bitwise Operators with Example to get every required things about Bitwise operators. Here is an example uses and_() method:

import operator

a, b = 60, 13
res = operator.and_(a, b)
print(res)

The output produced by above program will be 12. To learn how calculation on Bitwise AND operator gets performed, refer to its separate article as provided just above the program. But for now, here is another example program that may remind you about the operator:

import operator

a, b = 1, 0
print(a, "&", b, "=", operator.and_(a, b))
a, b = 0, 1
print(a, "&", b, "=", operator.and_(a, b))
a, b = 0, 0
print(a, "&", b, "=", operator.and_(a, b))
a, b = 1, 1
print(a, "&", b, "=", operator.and_(a, b))

The snapshot given below shows the output produced by this Python program:

python operator module and_ method

or_() Method

The or_() method works same as | operator. Here is an example uses this method:

import operator

a, b = 60, 13
res = operator.or_(a, b)
print(res)

Produces 61 as output. That is, while taking the Bitwise OR between binary equivalent of both values say 60 and 13, we'll get another binary value, that is of course equal to 61.

xor() Method

The xor() method works same as ^ operator. Here is an example program uses this method:

import operator

a, b = 60, 13
res = operator.xor(a, b)
print(res)

Produces 49 as output.

invert() Method

The invert() method works same as ˜. This method is used to invert the binary value or binary equivalent of a value. Unlike above methods, this method works on single argument. Here is an example uses invert() method:

import operator

a = 5
print(operator.invert(a))

Produces -6 as output.

lshift() Method

The lshift() works same as << operator. Here is an example uses lshift() method of operator module:

import operator

a, b = 14, 1
print(operator.lshift(a, b))

Produces 28 as output, because after shifting the binary equivalent of 14 to 1 position left, we'll get a binary value, that is equal to 28 (in decimal).

rshift() Method

The rshift() works same as >> operator. Here is an example uses rshift method:

import operator

a, b = 14, 1
print(operator.rshift(a, b))

Produces 7 as output, because after shifting the binary equivalent value (0000 1110) of 14 to 1 position left, we'll get a binary value (0000 0111), that is equal to 7 in decimal.

is_() Method

The is_() method of operator module works same as is identity operator. Here is an example uses is_() method:

import operator

a, b, = 10, 10
print(operator.is_(a, b))

a, b = "codes", "cracker"
print(operator.is_(a, b))

a, b = "codescracker", "codescracker"
print(operator.is_(a, b))

The snapshot given below shows the sample output produced by above program:

python operator module is_ method

is_not() Method

The is_not() works same as is not operator. Here is an example uses this method:

import operator

a, b, = 10, 10
print(operator.is_not(a, b))

a, b = "codes", "cracker"
print(operator.is_not(a, b))

a, b = "codescracker", "codescracker"
print(operator.is_not(a, b))

The output produced by this program looks like:

python operator module is_not method

contains() Method

The contains() method of operator module works same as in operator, checks whether an object is a member of any list, string, or tuple or not. Here is an example uses contains() method:

import operator

nums = [1, 2, 3, 4, 5]
val = 3

res = operator.contains(nums, val)
print(res)

print(operator.contains(nums, 6))

Here is the snapshot taken, of the output produced by above Python program:

python operator module contains method

The program given below shows the major and most popular use of contains() method in Python programming, that is to search an element:

import operator

nums = [1, 2, 3, 4, 5]

print("Enter an element to search: ")
val = int(input())

if operator.contains(nums, val):
    print("\nIt is available in the list")
else:
    print("\nThe element is not found in the list!")

And here is its sample run with user input, 10 as input:

python operator module contains method example

concat() Method

The concat() method of operator module works same as + while concatenating two strings. This method takes two arguments, and both argument must be of string type. Here is an example:

import operator

a = "codes"
b = "cracker"
res = operator.concat(a, b)
print(res)

The output produced by above program will be codescracker

getitem() Method

The getitem() method is used to get an element. The following code or statement:

val = operator.getitem(nums, index_number)

works similar to, or gets treated as:

val = nums[index_number]

Here is an example program uses getitem() method of operator module:

import operator

nums = [10, 20, 30, 40, 50]
print("Get element with Index Number: ", end="")
index_number = int(input())

val = operator.getitem(nums, index_number)
print("\nThe value at index", index_number, "is", val)

Here is its sample run with user input, 3 as index number to get the element at this index number:

python operator module getitem method

The same method, that is getitem() can also be used to get multiple items. To do this, use method in a way like getitem(nums, slice(i, j)) where all items from index i to j gets sliced from nums. The slicing performs in a way that the first index (i) is included whereas the second index (j) is excluded. That is, getitem(nums, slice(1, 3)) sliced elements at first, and second index only, but not of at third index. Here is an example:

import operator

nums = [10, 20, 30, 40, 50]
print(operator.getitem(nums,slice(1, 3)))

The output produced by above program will be [20, 30].

setitem() Method

The setitem() method (known as indexed assignment) is basically used to replace an element from the list. For example, the following statement:

operator.setitem(nums, index_number, new_value)

is similar to, or gets treated as:

nums[index_number] = new_value

Here is an example program uses setitem() method of operator module:

import operator

nums = [10, 20, 30, 40, 50]
operator.setitem(nums, 2, 3)
print(nums)

Here is its sample output:

python operator module setitem method

The setitem() method can also be used to assign multiple values at a time. This can be used using slice assignment. To do this, we need to use setitem() method as setitem(seq, slice(i, j), values), which is similar to, or gets treated as:

seq[i:j] = values

Here is an example program:

import operator

nums = [10, 20]
operator.setitem(nums, slice(2, 5), [30, 40, 50])
print(nums)

Produces [10, 20, 30, 40, 50] as output, since new 3 elements gets inserted using setitem() method.

delitem() Method

The delitem() method of operator module, also known as indexed deletion, used to delete an element. The following code that uses delitem() method:

operator.delitem(nums, index_number)

works similar to, or gets treated as:

del nums[index_number]

Here is an example program uses delitem() method:

import operator

nums = [10, 20, 30, 40, 50]
operator.delitem(nums, 2)
print(nums)

After executing above program, we'll get the following output:

python operator module delitem method

Just like setitem(), delitem() method can also be used to delete multiple elements at a time using slice deletion. This can be done using the same method in a way like delitem(seq, slice(i, j)), which works similar to:

del seq[i:j]

Here is an example program uses delitem() method to delete multiple items:

import operator

nums = [10, 20, 30, 40, 50]
operator.delitem(nums, slice(2, 5))
print(nums)

Produces following output:

python operator module delitem multiple method

Python Online Test


« Python Tutorial Python Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker