Site icon Machine Learning Projects

Python Program to Compute the Power of a Number – 2023

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Compute the Power of a Number.

Example 1: Calculate the power of a number using a while loop

# Python Program to Compute the Power of a Number

base = 5
exponent = 4

result = 1

while exponent != 0:
    result *= base
    exponent-=1

print("Answer = " + str(result))

Output

Answer = 625

Example 2: Calculate the power of a number using a for loop

base = 5
exponent = 4

result = 1

for exponent in range(exponent, 0, -1):
    result *= base

print("Answer = " + str(result))

Output

Answer = 625

Example 3: Calculate the power of a number using the pow() function

base = 5
exponent = -4

result = pow(base, exponent)

print("Answer = " + str(result))

Output

Answer = 0.0016

Check out our other python programming examples

Exit mobile version