Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)

10 Feb, 2020
Python in its definition provides methods to perform inplace operations, i.e doing assignment and computation in a single statement using “operator” module. For example,
x += y is equivalent to x = operator.iadd(x, y) 
Some Important Inplace operations :
1. iadd() :- 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.
2. iconcat() :- This function is used to concat one string at end of second.
# Python code to demonstrate the working of # iadd() and iconcat()  # importing operator to handle operator operationsimport operator  # using iadd() to add and assign valuex = operator.iadd(2, 3);  # printing the modified valueprint ("The value after adding and assigning : ", end="")print (x)  # initializing valuesy = "geeks"  z = "forgeeks"  # using iconcat() to concat the sequencesy = operator.iconcat(y, z)  # using iconcat() to concat sequences print ("The string after concatenation is : ", end="")print (y)
Output:
The value after adding and assigning : 5
The string after concatenation is : geeksforgeeks

3. isub() :- 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.
4. 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.
# Python code to demonstrate the working of # isub() and imul()  # importing operator to handle operator operationsimport operator  # using isub() to subtract and assign valuex = operator.isub(2, 3);  # printing the modified valueprint ("The value after subtracting and assigning : ", end="")print (x)  # using imul() to multiply and assign valuex = operator.imul(2, 3);  # printing the modified valueprint ("The value after multiplying and assigning : ", end="")print (x)
Output:
The value after subtracting and assigning : -1
The value after multiplying and assigning : 6

5. 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.
6. 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.
# Python code to demonstrate the working of # itruediv() and imod()  # importing operator to handle operator operationsimport operator  # using itruediv() to divide and assign valuex = operator.itruediv(10, 5);  # printing the modified valueprint ("The value after dividing and assigning : ", end="")print (x)  # using imod() to modulus and assign valuex = operator.imod(10, 6);  # printing the modified valueprint ("The value after modulus and assigning : ", end="")print (x)
Output:
The value after dividing and assigning : 2.0
The value after modulus and assigning : 4

YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=93uhgWJAZtY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Next Articles
Inplace Operators in Python | Set 2
Inplace vs Standard Operators
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python
Python | os.path.join() method