In-Place Operators in Python | Set 1 (iadd (), isub (), iconcat () …)



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.

# Python code to demonstrate how it works
# iadd () and iconcat ()

 
# import statement to handle statement operations

import operator

 
# using iadd () to add and assign a value

x = operator.iadd ( 2 , 3 ); 

 
# print the changed value

print ( "The value after adding and assigning:" , end = "")

print (x)

 
# initializing values ​​

y = "geeks"

 

z = "forgeeks"

 
# using iconcat () to combine sequences

y = operator.iconcat (y, z)

 
# using iconcat () to combine sequences

print ( "The string after concatenation is:" , end = "")

print (y)

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.

# Python code to demonstrate how it works
# isub () and imul ()

 
# import statement to handle statement operations

import operator

 
# using isub () to subtract and assign a value

x = operator.isub ( 2 , 3 ); 

 
# print the changed value

print ( "The value after subtracting and assigning:" , end = "")

print (x)

 
# using imul () to multiply and assign a value

x = operator.imul ( 2 , 3 ); 

 
# print the changed value

print ( "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 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.

# Python code to demonstrate how it works
# itruediv () and imod ()

 
# import statement to handle statement operations

import operator

 
# using itruediv () to divide and assign a value

x = operator.itruediv ( 10 , 5 ); 

 
# print the changed value

print ( "The value after dividing and assigning:" , end = "")

print (x)

 
# using imod () for the module and assigning a value

x = operator.imod ( 10 , 6 ); 

 
# print the changed value

print ( "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 

Following articles

  • Inplace versus standard operators
  • 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.