0
Exponentiation
What is the formula of exponentiation with repeated multiplication in python? Please
2 Respuestas
0
There are much more efficient ways to calculate exponents but you said "repeated multiplication" which sounds like you want to repeatedly multiply.
Do you want something like this?
def pow(base, exponent):
if exponent == 0:
return 1
else:
return base * pow(base, exponent - 1)
print(pow(2, 3)) # prints 8
Above is recursive but you could do the same with a for loop like this:
def pow(base, exponent):
result = 1
for i in range(exponent):
result *= base
return result
0
Terrence Newman If you're trying to figure out the operator syntax or built-in function for calculating exponential value in Python, see the options below:
1. Power Operator:
base ** exponent
result = 2 ** 5
# 32
2. Power Function:
pow(base, exponent)
result = pow(2, 5)
# 32