Regular operators perform a simple assignment task. On the other hand, Inplace operators behave similarly to regular operators, except they act differently for mutable and immutable targets.
In both cases, an assignment is required to store the value.
Case 1 : Fixed targets.
Fixed targets such as numbers, strings, and tuples. In-place operators behave the same as regular operators, i.e., only assignment is performed, no changes are made to the arguments passed.
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
Behavior of Inplace operators for mutable targets, such as lists and dictionaries are different from ordinary operators. Updates and assignments are performed in the case of mutable targets.
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 courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.