Python’s in-place operators, set 1 (including iadd(), isub(), iconcat(), and more) Read, Comment, and Engage in Instructional Videos.
Python, by its very nature, offers users the ability to carry out in-place operations, which entails doing both assignment and calculation inside the scope of a single statement by using the ” operator ” module. For
example,
x += y is equivalent to x = operator.iadd(x, y)
Significant in-place operations include the following: 1. The iadd() method is used to assign and add the current value. This function may be found in most programming languages. The “a plus = b” operation is being carried out here. When dealing with immutable containers, such as strings, integers, and tuples, assigning is not something that is done. 2. iconcat() — This method is used to concatenate one string onto the end of another string.
second.
|
Output:
The value after adding and assigning : 5 The string after concatenation is : geeksforgeeks
3. The isub() function is used to do the operations of assigning and subtracting the current value. The “a-=b” operation will be carried out by this step. When dealing with immutable containers, such as strings, integers, and tuples, assigning is not something that is done. 4. The imul() function is used to perform the operations of assigning and multiplying the current value. The “a*=b” operation will be performed using this operation. In the case of immutable containers, such as strings, integers, and dates, assigning is not carried out.
tuples.
|
Output:
The value after subtracting and assigning : -1 The value after multiplying and assigning : 6
5. itruediv() – This method is used to assign and divide the current value. itruediv() may be found in most modern programming languages. The “a/=b” operation will be carried out by this step. When dealing with immutable containers, such as strings, integers, and tuples, assigning is not something that is done. 6. The remaining may be assigned with the help of the imod() method, which also returns it. The “a%=b” operation will be performed using this operation. In the case of immutable containers, such as strings, integers, and dates, assigning is not carried out.
tuples.
|
Output:
The value after dividing and assigning : 2.0 The value after modulus and assigning : 4
Next
Articles