Python in its definition provides methods to perform inplace operations, i.e. doing assignments and computations in a single statement using an operator module.
Example
x += y is equivalent to x = operator.iadd(x, y)
Inplace Operators in Python
Below are some of the important Inplace operators in Python:
- iadd()
- isub()
- iconcat()
- imul()
- itruediv()
- imod()
iadd() in Python
This function is used to assign and add the current value. This operation does “a+=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers, and tuples.
Python3
import operator
x = operator.iadd(2, 3);
print ("The value after adding and assigning : ", end="")
print (x)
|
Output
The value after adding and assigning : 5
Python iconcat()
This function is used to concat one string at end of second. In the below example, we have used iconcat() function.
Python3
import operator
y = "geeks"
z = "forgeeks"
y = operator.iconcat(y, z)
print ("The string after concatenation is : ", end="")
print (y)
|
Output
The string after concatenation is : geeksforgeeks
isub() in Python
This function is used to assign and subtract the current value. This operation does “a-=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
import operator
x = operator.isub(2, 3);
print ("The value after subtracting and assigning : ", end="")
print (x)
|
Output
The value after subtracting and assigning : -1
Inplace Operator – imul()
This function is used to assign and multiply the current value. This operation does “a*=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
import operator
x = operator.imul(2, 3);
print ("The value after multiplying and assigning : ", end="")
print (x)
|
Output
The value after multiplying and assigning : 6
Python itruediv()
This function is used to assign and divide the current value. This operation does “a/=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
import operator
x = operator.itruediv(10, 5);
print("The value after dividing and assigning : ", end="")
print(x)
|
Output
The value after dividing and assigning : 2.0
Inplace Operator – imod()
This function is used to assign and return remainder . This operation does “a%=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
import operator
x = operator.imod(10, 6);
print ("The value after modulus and assigning : ", end="")
print (x)
|
Output
The value after modulus and assigning : 4
Related Articles
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
28 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...