Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
Last Updated :
28 Nov, 2023
Python in its definition provides methods to perform inplace operations, i.e. doing assignments and computations in a single statement using an operator module.
Example
x += y is equivalent to x = operator.iadd(x, y)
Inplace Operators in Python
Below are some of the important Inplace operators in Python:
- iadd()
- isub()
- iconcat()
- imul()
- itruediv()
- imod()
iadd() in Python
This function is used to assign and add the current value. This operation does "a+=b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers, and tuples.
Python3
# importing operator to handle operator operations
import operator
# using iadd() to add and assign value
x = operator.iadd(2, 3);
# printing the modified value
print ("The value after adding and assigning : ", end="")
print (x)
OutputThe value after adding and assigning : 5
Python iconcat()
This function is used to concat one string at end of second. In the below example, we have used iconcat() function.
Python3
# importing operator to handle operator operations
import operator
# initializing values
y = "geeks"
z = "forgeeks"
# using iconcat() to concat the sequences
y = operator.iconcat(y, z)
# using iconcat() to concat sequences
print ("The string after concatenation is : ", end="")
print (y)
OutputThe string after concatenation is : geeksforgeeks
isub() in Python
This function is used to assign and subtract the current value. This operation does "a-=b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
# importing operator to handle operator operations
import operator
# using isub() to subtract and assign value
x = operator.isub(2, 3);
# printing the modified value
print ("The value after subtracting and assigning : ", end="")
print (x)
OutputThe value after subtracting and assigning : -1
Inplace Operator - imul()
This function is used to assign and multiply the current value. This operation does "a*=b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
# importing operator to handle operator operations
import operator
# using imul() to multiply and assign value
x = operator.imul(2, 3);
# printing the modified value
print ("The value after multiplying and assigning : ", end="")
print (x)
OutputThe value after multiplying and assigning : 6
Python itruediv()
This function is used to assign and divide the current value. This operation does "a/=b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
# importing operator to handle operator operations
import operator
# using itruediv() to divide and assign value
x = operator.itruediv(10, 5);
# printing the modified value
print("The value after dividing and assigning : ", end="")
print(x)
OutputThe value after dividing and assigning : 2.0
Inplace Operator - imod()
This function is used to assign and return remainder . This operation does "a%=b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
Python3
# importing operator to handle operator operations
import operator
# using imod() to modulus and assign value
x = operator.imod(10, 6);
# printing the modified value
print ("The value after modulus and assigning : ", end="")
print (x)
OutputThe value after modulus and assigning : 4
Related Articles
Similar Reads
Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),â¦) Inplace Operators in Python | Set 1(iadd(), isub(), iconcat()â¦) More functions are discussed in this articles. 1. ixor() :- This function is used to assign and xor the current value. This operation does "a^ = b" operation. Assigning is not performed in case of immutable containers, such as strings,
3 min read
Operator Functions in Python | Set 1 Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module "operator". Some of the basic functions are covered in this article. 1. add(a, b) :- This function returns addition of the given arguments. Operation - a + b. 2. sub(a, b) :- This func
5 min read
Operator Functions in Python | Set 2 Operator Functions in Python | Set 1 More functions are discussed in this article. 1. setitem(ob, pos, val) :- This function is used to assign the value at a particular position in the container. Operation - ob[pos] = val 2. delitem(ob, pos) :- This function is used to delete the value at a particul
5 min read
Inplace vs Standard Operators in Python Inplace Operators - Set 1, Set 2Normal operators do the simple assigning job. On other hand, Inplace operators behave similarly 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, return
3 min read
Comparison Operators in Python Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
4 min read