Inplace vs Standard Operators in Python - GeeksforGeeks

10 Aug, 2021
Inplace Operators – Set 1, Set 2Normal operators do the simple assigning job. On other hand, Inplace operators behave similarly to normal operators except that they act in a different manner in case of mutable and Immutable targets.  
The _add_ method, does simple addition, takes two arguments, returns the sum, and stores it in another variable without modifying any of the arguments.
On the other hand, _iadd_ method also takes two arguments, but it makes an in-place change in 1st argument passed by storing the sum in it. As object mutation is needed in this process, immutable targets such as numbers, strings, and tuples, shouldn’t have _iadd_ method.
Normal operator’s “add()” method, implements “a+b” and stores the result in the mentioned variable.
Inplace operator’s “iadd()” method, implements “a+=b” if it exists (i.e in case of immutable targets, it doesn’t exist) and changes the value of the passed argument. But if not, “a+b” is implemented.
Case 1: Immutable Targets. In Immutable targets, such as numbers, strings, and tuples. Inplace operators behave the same as normal operators, i.e only assignment takes place, no modification is taken place in the passed arguments. 
Python
# Python code to demonstrate difference between # Inplace and Normal operators in Immutable Targets  # importing operator to handle operator operationsimport operator  # Initializing valuesx = 5y = 6a = 5b = 6  # using add() to add the arguments passed z = operator.add(a,b)  # using iadd() to add the arguments passed p = operator.iadd(x,y)  # printing the modified valueprint ("Value after adding using normal operator : ",end="")print (z)  # printing the modified valueprint ("Value after adding using Inplace operator : ",end="")print (p)  # printing value of first argument# value is unchangedprint ("Value of first argument using normal operator : ",end="")print (a)  # printing value of first argument# value is unchangedprint ("Value of first argument using Inplace operator : ",end="")print (x)
Output:
Value after adding using normal operator : 11
Value after adding using Inplace operator : 11
Value of first argument using normal operator : 5
Value of first argument using Inplace operator : 5
Case 2: Mutable Targets The behavior of Inplace operators in mutable targets, such as lists and dictionaries, is different from normal operators. The updation and assignment both are carried out in case of mutable targets. 
Python
# Python code to demonstrate difference between # Inplace and Normal operators in mutable Targets  # importing operator to handle operator operationsimport operator  # Initializing lista = [1, 2, 4, 5]  # using add() to add the arguments passed z = operator.add(a,[1, 2, 3])  # printing the modified valueprint ("Value after adding using normal operator : ",end="")print (z)  # printing value of first argument# value is unchangedprint ("Value of first argument using normal operator : ",end="")print (a)  # using iadd() to add the arguments passed # performs a+=[1, 2, 3]p = operator.iadd(a,[1, 2, 3])  # printing the modified valueprint ("Value after adding using Inplace operator : ",end="")print (p)  # printing value of first argument# value is changedprint ("Value of first argument using Inplace operator : ",end="")print (a)
Output:  
Value after adding using normal operator : [1, 2, 4, 5, 1, 2, 3]
Value of first argument using normal operator : [1, 2, 4, 5]
Value after adding using Inplace operator : [1, 2, 4, 5, 1, 2, 3]
Value of first argument using Inplace operator : [1, 2, 4, 5, 1, 2, 3]
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
punamsingh628700
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Python | Get dictionary keys as a list
Bar Plot in Matplotlib
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
loops in python
Python - Call function from another file
Ways to filter Pandas DataFrame by column values
Python | Convert set into a list
Python program to find number of days between two given dates