How can I rewrite this for use in a for loop?
# X to the Y
result = (X**Y)
print (result)
I know how to perform the for loop function but I can't remember how to write the exponents.
How can I rewrite this for use in a for loop?
# X to the Y
result = (X**Y)
print (result)
I know how to perform the for loop function but I can't remember how to write the exponents.
It seems to me that there are several ways of calculating an exponent in Python.
Some are covered in answers to this question.
# use X ** Y
print(X ** Y)
# use math.pow
print(math.pow(X,Y))
# use numpy
print(np.exp(X,Y))
# use a for loop
def pow(base, exponent): # integer exponent only
ret = 1
for _ in range(exponent):
ret *= base
return ret
print(pow(X,Y))