Python Program to Compute the Power of a Number

To understand this example, you should have the knowledge of the following Python programming topics:


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

base = 3
exponent = 4

result = 1

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

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

Output

Answer = 81

In this program, base and exponent are assigned values 3 and 4 respectively.

Using the while loop, we keep on multiplying the result by base until the exponent becomes zero.

In this case, we multiply result by base 4 times in total, so result = 1 * 3 * 3 * 3 * 3 = 81.


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

base = 3
exponent = 4

result = 1

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

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

Output

Answer = 81

Here, instead of using a while loop, we've used a for loop.

After each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times.

Both programs above do not work if you have a negative exponent. For that, you need to use the pow() function in the Python library.

Also Read: Python range()


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

base = 3
exponent = -4

result = pow(base, exponent)

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

Output

Answer = 0.012345679012345678

pow() accepts two arguments: base and exponent. In the above example, 3 raised to the power -4 is calculated using pow().


Also Read:

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to calculate the power of a number.

  • For example, 2 and 3, the output should be 8.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges