Does exponentiation in Python work with negative exponents?


#1

Question

In this exercise, the exponent operator is used for positive exponents. Does exponentiation work with negative exponents as well?

Answer

Yes, in Python, exponentiation does work with negative exponent values, the same way it would apply in mathematics. When performing exponentiation with a negative exponent, it is the same as getting the inverse of exponentiation done with the exponent as a positive value.

Example code

base = 2
exp = -1

# Both of these results will be 0.5
result1 = base ** exp
result2 = 1 / (base ** -(exp))