Python Program to find Power of a Number

Write a Python Program to find the Power of a Number using a For Loop, While Loop, and pow function with an example. Here, our task is to find the power of a number, meaning raising the base value to the power of an exponent. For example, 32 = 9, where 3 is the base, and 2 is the exponent.

Python Program to find Power of a Number

This Python program allows the user to enter any numerical value or exponent. Next, this program finds the power of a number using For Loop. Check out the Python For Loop.

number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = 1

for i in range(1, exponent + 1):
    power = power * number
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))

The Power of a Number using for loop output.

 Please Enter any Positive Integer : 3
 Please Enter Exponent Value : 4
The Result of 3 Power 4 = 81

Python Program to find the Power of a Number using While loop

This power of a number program is the same as above, but this time, we are using While Loop. Check out the Python while Loop.

number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))

power = 1
i = 1

while(i <= exponent):
    power = power * number
    i = i + 1
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))

using while loop output

 Please Enter any Positive Integer : 10
 Please Enter Exponent Value : 5
The Result of 10 Power 5 = 100000

Program to find the Power of a Number using pow function

This Python program example accepts the number and an exponent from the user. Next, we are using the built-in math function called pow to find the power of a number. Please refer to the Python pow from the available Python math functions.

import math

number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))

power = math.pow(number, exponent)
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
Program to find Power of a Number

Apart from the math.pow(), we can use the regular pow() function to find the power of a given number.

b = 5
e = 4
print(pow(b, e))
625

Python Program to find power of a number using the ** operator

The Python Arithmetic operators provide an exponentiation operator (**). We can use the exponentiation operator (**) to calculate the power of a given number. It is the simplest approach among all the solutions provided here.

53 = 5 * 5 * 5 = 125

b = 5
e = 3
res = b ** e
print(res)
125

Calculate the power of a number using recursion

The following program is the simplest approach to calculating the power of a given number by calling the function recursively.

def calPower(base, exp):
if exp == 0:
return 1
return base * calPower(base, exp - 1)

b = 5
e = 2
print(calPower(b, e))
25

The above approach is not suitable for larger numbers. The program below is an improved version for calculating the power of a number. Here, we find half of the number. Next, calculate the power separately for even and odd numbers.

def calPower(base, exp):
if exp == 0:
return 1
mid = calPower(base, exp//2)
if exp % 2 == 0:
return mid * mid
else:
return base * mid * mid

b = 5
e = 4
print(calPower(b, e))
625

Find the power of a number using Numpy

When you work with the NumPy module, use the power() function to calculate the power of a given number.

import numpy as np
b = 3
e = 4
print(np.power(b, e))
81

Also Read