Tag Archives: exponent operator

Python Common Exponent Mistake

I was just learning some Python when I ran across a problem. So I thought I’d share the solution to that problem here in case any amateur programmers are running across it during their studies:

print 8^3

Was giving me the answer 11 when in actuality I wanted 8 raised to the 3rd power (8*8*8), which should yield 512.

This is best accomplished by using the ** operator instead of the ^ typically common in math.

print 8**3

Stack Overflow forum members helped me realize that ^ in Python was used as a Bitwise XOR while ** was actually used for exponents. So don’t confuse the tw

print 8**3 <= 8*8*8 # TRUE - Both are equal.

Instead Bitwise XOR enable you to input actual math operations and receive an output in binary.

If you’d like to read Python’s complicated page in regards to Expressions, and in particular the exponent operator, feel free to click here. Similarly if you’d like to learn more about Python’s Bitwise XOR click here.

Sources:

Python doc on Powers Operators:

http://docs.python.org/reference/expressions.html#the-power-operator

Python doc on Bitwise XOR:

http://wiki.python.org/moin/BitwiseOperators