In this article, we will learn about some of the inplace operators available in Python 3.x. Or earlier.
Python provides methods to perform inplace operations, i.e assignment and computation simultaneously using in a single statement with the help of “operator” module. Here we will discuss about iadd() , isub() & iconcat() functions .
iadd()
This function allows us to assign and add current value. This operation behaves like “a+=b” operation. Assigning can’t be performed in case of immutable data types, such as strings and tuples.
Example
import operator as op # using iadd() to add int1 = op.iadd(786,0); # displaying value print ("The value : ", end="") print (int1)
Output
The value : 786
isub()
This function allows us to assign and subtract current value. This operation behaves like “a-=b” operation. Assigning can’t be performed in case of immutable data types, such as strings and tuples.
Example
# using isub() to subtract int2 = op.isub(57,34) print ("The value : ", end="") print (int2)
Output
: The value : 23
iconcat()
This function allows us to concatenate one string at end of the second string working like an addition operator for strings.
Example
str1 = "tutorials" str2 = "point" # using iconcat() to concatenation str1 = op.iconcat(str1, str2) # displaying value print ("The string becomes: ", end="") print (str1)
Output
The string becomes: tutorialspoint
Conclusion
In this article, we learned about the usage and implementation of Inplace Operators in Python - iadd(), isub(), iconcat().