Exponentiation using builtins and pow
suggest changeExponentiation can be used by using the builtin pow-function or the ** operator:
- Negative base and a fractional exponent. The result is complex. Before Python 3, this raised a
ValueError:
(-2) ** (0.5) # also (-2.) ** (0.5)
# Out: (8.659560562354934e-17+1.4142135623730951j) (result is complex)
The operator module contains two functions that are equivalent to the **-operator:
import operator
operator.pow(4, 2) # 16
operator.__pow__(4, 3) # 64
or one could directly call the __pow__ method:
val1, val2 = 4, 2
val1.__pow__(val2) # 16
val2.__rpow__(val1) # 16
# in-place power operation isn't supported by immutable classes like int, float, complex:
# val1.__ipow__(val2)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents