How to find power of a number in Python?


In this article, we will show you how to find the power of a number in python. Below are the various methods to accomplish this task −

  • Using for loop

  • Using Recursion

  • Using pow() function

  • Using ** operator

Using for loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a function findPower() that returns the power of a Number. The function accepts number, and exponent value as parameters.

  • Take a variable that stores the result and initialize its value with 1.

  • To obtain the power, multiply the given number by the result exponent. We use the below loop to multiply the exponent number of times

  • Use the for loop, to traverse in the range 1 to given exponent+1.

  • Inside the loop, Multiply the result by the given number

  • Use the return keyword to return the resultant power.

  • Create 2 separate variables for storing input number and exponent values.

  • Call the above-defined findPower() function by passing the input number, and exponent values as arguments to it and print the returned result of function.

  • In the same way, calculating for other numbers

Example

The following program returns the power of a number using the for loop–

# creating a function that returns the power of a Number # It accepts Number, and exponent values as parameters def findPower(number, exponent): # intializing a variable with 1 (it stores the resultant power) resultPower =1 # traversing in the range from 1 to given exponent+1 for i in range(1, exponent+1): # Multiplying the result with the given number resultPower=resultPower*number # returning the resultant power return resultPower # input number, exponent values number = 5 exponent = 3 # calling the findPower() function by passing the number,exponent values # as arguments print("Value of",number,"raised to the power",exponent,"(",number,"^",exponent,") =", findPower(number,exponent)) #, In the same way, calculating for other numbers number = 4 exponent = 0 print("Value of",number,"raised to the power",exponent,"(",number,"^",exponent,") =", findPower(number,exponent))

Output

On executing, the above program will generate the following output −

Value of 5 raised to the power 3 ( 5 ^ 3 ) = 125
Value of 4 raised to the power 0 ( 4 ^ 0 ) = 1

Using Recursion

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a function findPower() that returns the power of a Number. The function accepts number, power/exponent value as parameters

  • Use the if conditional statement, to check whether the exponent value passed is equal to 0.

  • Return 1 if the condition is true i,e, exponent value is 0.

  • Subtract the exponent value by 1 and return the resultant power using recursive logic.

  • Call the above-defined findPower() function by passing the input number, and exponent values as arguments to it and print the returned result of function.

Example

The following program returns the power of a number using the recursive logic –

# creating a function that returns the power of a Number # It accepts Number, power/exponent value as parameters def findPower(number, exponent): # checking whether the exponent value passed is equal to 0 if exponent == 0: # returning 1 if the condition is true return 1 # Subtract the exponent value by 1 and return the resultant power using recursive logic return number * findPower(number, exponent-1) # input number, exponent values number = 5 exponent = 3 # calling the findPower() function by passing the number, exponent values # as arguments print("Value of",number,"raised to the power",exponent,"(",number,"^",exponent,") =", findPower(number,exponent))

Output

On executing, the above program will generate the following output −

Value of 5 raised to the power 3 ( 5 ^ 3 ) = 125

Using pow() function

In Python, the pow() function calculates the power of any positive integer.

It returns the value of x to the power of y (x^y).

Syntax

pow(x,y)

Parameters

  • x - It is the numerical value (base value)

  • y - It is the power of numerical value (exponent value)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input number.

  • Create another variable to store the exponent/power value.

  • Use the pow() function to print the resultant power of a number i.e, inputNumber ^ inputPower by passing the number, and exponent values as arguments to it and print the resultant power.

Example

The following program returns the power of a number using the pow() function –

# input number inputNumber = 5 # input exponent value inputPower = 3 # printing the resultant power value using the pow() function print("Value of 5 raised to the power 3(5^3)=", pow(inputNumber, inputPower))

Output

On executing, the above program will generate the following output −

Value of 5 raised to the power 3(5^3)= 125

Using ** operator

The exponential operator(**) can be used to calculate the power of a number.

Since importing a module or calling a function is not necessary, this is the most convenient to use.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input number.

  • Create a variable to store the exponent/power value.

  • Use the exponential operator(**) to print the resultant power of a number i.e, inputNumber ^ inputPower.

Example

The following program returns the power of a number using the ** operator –

# input number inputNumber = 6 # input exponent value inputPower = 2 # printing the resultant power of a number using exponential operator(**) print("Value of 6 raised to the power 2(6^2)=", inputNumber**inputPower)

Output

On executing, the above program will generate the following output –

Value of 6 raised to the power 2(6^2)= 36

Conclusion

In this article, we learned four different Python methods for calculating the power of a given number and exponent. We also learned how to calculate power without the use of built-in functions.

Updated on: 28-Oct-2022

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements