Get the power of a number using recursion in Python

A recursive function is a function that continuously calls itself. Here, in this tutorial, we are seeing how to find the power of a number using a recursive function in Python.

In this tutorial, we will code a very basic beginner’s problem – calculate the power of a number using recursion in Python. Power of a number also called exponent of a number, is given as be called as “b to the power e”, where b is the base and e is the exponent. The exponent operator in Python is two stars (“**“).

Mathematically, the exponent of a base is computed by multiplying the base with itself exponent number of times. For example, let’s take base 2 and exponent 5. The power of 2 with exponent 5 or “2 to the power 5” will be 32.

Base = 2
Exponent = 5
2 ** 5 = 2 x 2 x 2 x 2 x 2
= 32

The following mathematical operation can be programmed using the recursion technique.

Recursion is a “divide and conquer” technique. It divides the problem into smaller subproblems by recursively calling the same function again and again with different inputs until it reaches a base case. A recursive function has two important parts- the recursive case and the base case.

Recursive case is the part responsible for recursively calling the function with different inputs. The base case is very important as the function halts only when it satisfies the base case. Without it, the function will go on recursively calling itself an infinite number of times until it runs out of memory.

Let us see the code to calculate the power of a number using recursion in Python.

Python Code to Calculate the Power of a Number using Recursion

# calculate the power of the given base
def power(base: int, exp: int):
    # base case
    if exp == 1:
        return base
    # recursive case
    return base*power(base,exp-1)

# input statements to get the base and exponent    
base = int(input("Enter the base: "))
exp = int(input("Enter the exponent: "))

# variable to store the power of the given base
power_of_base = power(base, exp)

print("\n{} to the power {} is {}.".format(base, exp, power_of_base))

 

Let’s check the power of 2 with exponents 5, 10, and 100 respectively.

Enter the base: 2

Enter the exponent: 5

2 to the power 5 is 32.
Enter the base: 2

Enter the exponent: 10

2 to the power 10 is 1024.
Enter the base: 2

Enter the exponent: 100

2 to the power 100 is 1267650600228229401496703205376.

Using this code you can calculate the power of any number with any exponent. There are two more methods to calculate the power of a number- using direct ** operator and using for loop. Let us see the code using these two methods also.

Using the Exponent operator:

# input statements to get the base and exponent 
base = int(input("Enter the base: "))
exp = int(input("Enter the exponent: "))

power_of_base = base ** exp

print("{} to the power {} is {}.".format(base, exp, power_of_base))
Enter the base: 3

Enter the exponent: 4

3 to the power 4 is 81.

 

Using For loop:

# input statements to get the base and exponent 
base = int(input("Enter the base: "))
exp = int(input("Enter the exponent: "))

# variable to store the power of the given base
power_of_base = 1

# calculate the power of the given base
for i in range(exp):
    power_of_base = power_of_base * base
    
print("\n{} to the power {} is {}.".format(base, exp, power_of_base))
Enter the base: 3

Enter the exponent: 4

3 to the power 4 is 81.

Want to add your thoughts? Need any further help? Leave a comment and I will get back to you ASAP 🙂

Another example:

The function that we are making is going to take a base number and an exponent as the argument and the function further works as follows:

  1. Pass the arguments to the recursive function to find the power of the number.
  2. Give the base condition for the case where we have the exponent argument as 1.
  3. If the exponent is not equal to 1, return the base multiplied by the function with base and exponent minus 1 as a parameter.
  4. The function calls itself until the exponent value is 1.
  5. Print the power of the given base number.
def power(base,expo):
    if(expo==1):
        return(base)
    if(expo!=1):
        return(base*power(base,expo-1))
base=5
expo=3
print("Result:",power(base,expo))
base=12
expo=1
print("Result:",power(base,expo))

Output:

Result: 125
Result: 12

Here for the first set of inputs, the function runs recursively whereas, in the second set of inputs the base value is 1 and therefore, the first if the condition is satisfied and output comes.

Leave a Reply

Your email address will not be published. Required fields are marked *