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.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically