Python provides methods in its definition to perform operations in place, i.e. perform assignment and evaluation in one expression using the operator module. For example,
x + = y is equivalent to x = operator.iadd (x, y)
Some important in-place operations :
1. iadd () : — this function is used to assign and add the current value . This operation performs the operation " a + = b ". The not assignment is true for immutable containers such as strings, numbers, and tuples.
2. iconcat () : — This function is used to concatenate one line at the end of a second.
|
Output:
The value after adding and assigning: 5 The string after concatenation is: pythonengineering
3. isub () : — This function is used to assign and subtract the current value . This operation performs the " a- = b " operation. The not assignment is true for immutable containers such as strings, numbers, and tuples.
4. imul () : — This function is used to assign and multiply the current value . This operation performs the operation " a * = b ". The not assignment is true for immutable containers such as strings, numbers, and tuples.
|
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 performs the " a / = b " operation. The not assignment is true for immutable containers such as strings, numbers, and tuples.
6. imod () : — This function is used to assign and return the remainder . This operation performs the operation " a% = b ". The not assignment is true for immutable containers such as strings, numbers, and tuples.
|
Output:
The value after dividing and assigning: 2.0 The value after modulus and assigning: 4
Following articles
This article courtesy of Manjit 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.