0
0
PythonProgramBeginner · 2 min read

Python Program to Find Power of Number Using Recursion

You can find the power of a number using recursion in Python by defining a function like def power(base, exp): return 1 if exp == 0 else base * power(base, exp - 1) which multiplies the base number by itself exp times recursively.
📋

Examples

Inputbase=2, exp=3
Output8
Inputbase=5, exp=0
Output1
Inputbase=3, exp=4
Output81
🧠

How to Think About It

To find the power of a number using recursion, think of the problem as multiplying the base number by itself repeatedly. The base case is when the exponent is zero, which always returns 1. For other cases, multiply the base by the result of the function called with the exponent decreased by one.
📐

Algorithm

1
Get the base number and exponent as input.
2
Check if the exponent is zero; if yes, return 1.
3
Otherwise, multiply the base by the result of the function called with exponent minus one.
4
Return the result.
💻

Code

python
def power(base, exp):
    if exp == 0:
        return 1
    else:
        return base * power(base, exp - 1)

print(power(2, 3))  # Output: 8
Output
8
🔍

Dry Run

Let's trace power(2, 3) through the code

1

Initial call

power(2, 3) checks if exp == 0 (false), so returns 2 * power(2, 2)

2

Second call

power(2, 2) checks if exp == 0 (false), so returns 2 * power(2, 1)

3

Third call

power(2, 1) checks if exp == 0 (false), so returns 2 * power(2, 0)

4

Base case

power(2, 0) returns 1 because exp == 0

5

Return values

power(2, 1) returns 2 * 1 = 2 power(2, 2) returns 2 * 2 = 4 power(2, 3) returns 2 * 4 = 8

CallexpReturn value
power(2, 0)01
power(2, 1)12
power(2, 2)24
power(2, 3)38
💡

Why This Works

Step 1: Base case

When the exponent is zero, the function returns 1 because any number to the power of zero is 1.

Step 2: Recursive call

For exponents greater than zero, the function calls itself with the exponent decreased by one.

Step 3: Multiplication

Each recursive call multiplies the base number by the result of the smaller problem, building up the final power value.

🔄

Alternative Approaches

Using built-in pow() function
python
def power(base, exp):
    return pow(base, exp)

print(power(2, 3))  # Output: 8
This is the simplest and fastest method but does not demonstrate recursion.
Using tail recursion
python
def power(base, exp, result=1):
    if exp == 0:
        return result
    else:
        return power(base, exp - 1, result * base)

print(power(2, 3))  # Output: 8
Tail recursion can be more efficient but Python does not optimize tail calls.

Complexity: O(n) time, O(n) space

Time Complexity

The function calls itself once per decrement of the exponent, so it runs in linear time relative to the exponent.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used is proportional to the exponent.

Which Approach is Fastest?

Using Python's built-in pow() is fastest and uses optimized C code, while recursion is educational but slower and uses more memory.

ApproachTimeSpaceBest For
Recursive functionO(n)O(n)Learning recursion and small exponents
Built-in pow()O(1) or optimizedO(1)Fastest calculation for any exponent
Tail recursionO(n)O(n)Conceptual efficiency but no Python optimization
💡
Always define a clear base case in recursion to avoid infinite calls.
⚠️
Forgetting the base case causes infinite recursion and crashes the program.