Inplace Operator Functions in Python | FACE Prep

Unlike the inbuilt Python operators & operator functions, the Inplace operator functions perform computation & assignment in a single statement. For example, the standard operator functions like add (), mul () etc take two parameters, perform the operation of them & return the resultant. They do not modify the parameters or arguments. But, this is slightly different in the case of Inplace operator functions.

Let’s look understand these inplace operator functions with a few examples.

Note: These Inplace operator functions can be used by importing the “operator” module.

Some Important Inplace Operator Functions with Examples

1) iadd (a, b)

This function first performs the addition of a and b. It then assigns this resultant value to a. However, assigning doesn’t take place if a & b are of immutable data types like Numbers, Strings and Tuples. On the whole, this function performs the operation of a += b

from operator import *
num1, num2 = 5, 3
print (iadd(num1, num2))
print (num1)
Output:
8
5

Explanation: As you can see, numbers are an immutable datatype and hence assignment did not take place.

from operator import *
list1 = ['FACE']
list2 = ['Prep']
print (iadd(list1, list2))
print (list1)

Output:
[‘FACE’, ‘Prep’]
[‘FACE’, ‘Prep’]

2) isub (a, b)

This function subtracts the value of b from a and then assigns the result of the subtraction to a. This function performs the operation of the expression a -= b. Similar to iadd(, the assignment of the resultant to a doesn’t happen in case of immutable data types like Strings, Numbers & Tuples.

from operator import *
num1, num2 = 5, 3
print (isub(num1, num2))
print (num1)
Output:
2
5

3) imul (num1, num2)

This function does the operation of a *= b. It multiplies b with a and stores the result in a. Here as well, assigning is not performed if in case a and b are of immutable data types/objects, such as strings, numbers and tuples.
from operator import *
num1, num2 = 5, 3
print(imul(num1, num2))
Output:
15

4) itruediv (num1, num2)

This function does the operation of the expression num1 /= num2
from operator import *
num1, num2 = 5, 3
print(itruediv(num1, num2))
Output:
1.66666666667

5) imod (num1, num2)

This function does the operation of the expression num1 %= num2.
from operator import *
num1, num2 = 5, 3
print(imod(num1, num2))
Output:
2

6) iconcat (num1, num2)

This function is used to join or concatenate two strings and assign the resultant string to the first operand. For example,

from operator import *
list1 = ['FACE']
list2 = ['Prep']
print(iconcat(list1, list2))
print(list1)
Output:
['FACE', 'Prep']
['FACE', 'Prep']

Inplace Operator Functions in Python FAQs

Inplace operators perform computation and assignment in a single statement. Post the computation, the resultant is assigned to the first operator. Hence this first operator should be of a mutable datatype.

For example, if iadd (a, b) is used, firstly a and b get added and the resultant is stored in the first operator i.e a. But since a is a number data type, it is immutable, this means the assignment doesn’t take place. Hence inplace operators are not suitable while working with immutable data types.