Python Power Operator

The Python power operator lets you do exponential math:

>>> 5**3
125

It yields the left argument raised to the power of the right argument.

Using floats:

>>> 3.2 ** 2.3
14.515932837559118
>>> 2.0 ** 2
4.0

If any of the numbers in the operation are floats, Python will return a float.

You can take a negative number to a positive power, and vice versa:

>>> -2.0 ** 3
-8.0
>>> 2.0 ** -3
0.125

But taking 0 to a negative power raises a ZeroDivisionError.

>>> 0 ** -3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: 0.0 cannot be raised to a negative power

Leave a Reply