I want to find the 5th power of a number given by the user. Can i do this without typing:
ans = n * n * n * n * n
and can it be used for higher powers?
Use the **
operator for exponents:
print(12**5)
>>> 224832
We can use this more generally using variables.
exponent = 5
n = 12
ans = n ** exponent
print(ans)
>>> 248832
Other Methods
Since the **
operator caps at an exponent of 256, we can use the built-in pow()
function for larger exponents. However, these numbers can be very large.
power = pow(base, exponent)
The math
and numpy
libraries also can do this using the math.pow()
and numpy.power()
functions.