Python Mystery Powers
-
Beginning Python programmers can be easily confused by the numeric operator for exponents. In many languages, the statement:
print(2^2)
Will return
4
, or2
raised to the power of2
. However, the^
operator in Python has a completely different meaning. It actually returns the bitwise exclusive OR of the two numbers. For the example of2^2
, the number0
will be returned instead of the expected4
.To express exponents in Python, use either:
x = pow(a,b) x = a**b