Inplace Operators in Python - ixor(), iand(), ipow()

PythonServer Side ProgrammingProgramming

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 ixor(), iand(), ipow() functions .

ixor()

This function allows us to assign and xor 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 ixor() to xor
int1 = op.ixor(786,12);

# displaying value
print ("The value : ", end="")
print (int1)

Output

The value : 798

iand()

This function allows us to assign and bitwise& 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 iand() to bitwise&
int2 = op.iand(57,34)

print ("The value : ", end="")
print (int2)

Output

The value : 32

ipow()

This function allows us to assign and exponentiate the 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 ipow() to exponentiate
int2 = op.ipow(3,2)

print ("The value : ", end="")
print (int2)

Output

The value : 9

Conclusion

In this article, we learned about the usage and implementation of Inplace Operators in Python - ixor(), iand(), ipow().

raja
Published on 07-Aug-2019 10:57:12
Advertisements