# Inplace vs Standard Operators in Python

Inplace Operators – Set 1, Set 2

Normal operators do the simple assigning job. On other hand, Inplace operators behave similar 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 other variable without modifying any of the argument.
• On the other hand, _iadd_ method also takes two arguments, but it makes 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 passed argument. But if not, “a+b” is implemented.
• In both the cases assignment is required to do to store the value.

Case 1 : Immutable Targets.
In Immutable targets, such as numbers, strings and tuples. Inplace operator behave same as normal operators, i.e only assignment takes place, no modification is taken place in the passed arguments.

 `# Python code to demonstrate difference between  ` `# Inplace and Normal operators in Immutable Targets ` ` `  `# importing operator to handle operator operations ` `import` `operator ` ` `  `# Initializing values ` `x ``=` `5` `y ``=` `6` `a ``=` `5` `b ``=` `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 value ` `print` `(``"Value after adding using normal operator : "``,end``=``"") ` `print` `(z) ` ` `  `# printing the modified value ` `print` `(``"Value after adding using Inplace operator : "``,end``=``"") ` `print` `(p) ` ` `  `# printing value of first argument ` `# value is unchanged ` `print` `(``"Value of first argument using normal operator : "``,end``=``"") ` `print` `(a) ` ` `  `# printing value of first argument ` `# value is unchanged ` `print` `(``"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 behaviour of Inplace operators in mutable targets, such as list and dictionaries, is different from normal operators.The updation and assignment both are carried out in case of mutable targets.

 `# Python code to demonstrate difference between  ` `# Inplace and Normal operators in mutable Targets ` ` `  `# importing operator to handle operator operations ` `import` `operator ` ` `  `# Initializing list ` `a ``=` `[``1``, ``2``, ``4``, ``5``] ` ` `  `# using add() to add the arguments passed  ` `z ``=` `operator.add(a,[``1``, ``2``, ``3``]) ` ` `  `# printing the modified value ` `print` `(``"Value after adding using normal operator : "``,end``=``"") ` `print` `(z) ` ` `  `# printing value of first argument ` `# value is unchanged ` `print` `(``"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 value ` `print` `(``"Value after adding using Inplace operator : "``,end``=``"") ` `print` `(p) ` ` `  `# printing value of first argument ` `# value is changed ` `print` `(``"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]
```

Python
{"ft_lang_label":"__label__en","ft_lang_prob":0.70723444,"math_prob":0.8968489,"size":3790,"snap":"2021-43-2021-49","text_gpt3_token_len":963,"char_repetition_ratio":0.20338088,"word_repetition_ratio":0.43195266,"special_character_ratio":0.26781002,"punctuation_ratio":0.14772727,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99691933,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-18T10:35:02Z\",\"WARC-Record-ID\":\"<urn:uuid:c582034c-5983-49ca-9ab0-3cc4dee5d6ad>\",\"Content-Length\":\"30926\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:387f9805-f2e0-46c0-91f6-287e0385eb79>\",\"WARC-Concurrent-To\":\"<urn:uuid:a73e7c7d-33fd-47af-9249-43a88fcb8d91>\",\"WARC-IP-Address\":\"104.21.79.77\",\"WARC-Target-URI\":\"https://tutorialspoint.dev/language/python/inplace-vs-standard-operators-python\",\"WARC-Payload-Digest\":\"sha1:BTN33ELOF6Z6P453SM6GC37YJ5HYDOEW\",\"WARC-Block-Digest\":\"sha1:WHZSGATVMTAWJWSJ2EBSMHHC5ARUKWR5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585201.94_warc_CC-MAIN-20211018093606-20211018123606-00158.warc.gz\"}"}