Change language

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

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.

In-Place Operators in Python | Set 1 (iadd (), isub (), iconcat () ...) File handling: Questions

In-Place Operators in Python | Set 1 (iadd (), isub (), iconcat () ...) iadd: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method