How to find the power of a number in Python

pow() function

  1. Import the math module as shown below in your Python environment.

  2. Use the pre-defined math.pow(number,exponent) function to find the power of the number.

import math
print(math.pow(4,2))

Iterative approach

  1. Define the function, which takes two arguments:

    • number
    • exponent/power
  2. Find the power of a number: multiply the number with itself the same number of times as the exponent’s value. We use the for…loop to achieve this task and store the result in the res variable.

  3. Return the result stored in res, which is the required power of a number.

  4. Finally, display the output.

def power(n,e):
res=1
for i in range(e):
res *= n
return res
print(power(4,2))

Recursive approach

  1. Define the function, which takes two arguments:

    • number
    • exponent/power
  2. If the exponent/power is 0, then return 1.

  3. If the exponent/power is 1 then return n, i.e., the number itself.

  4. Otherwise, multiply the current number and call the power(n,e) function recursively with e (exponent/power) decremented by 1n*power(n, e-1) each time the function is called.

  5. Finally, display the output, which is the desired power of the number.

def power(n, e):
if e == 0:
return 1
elif e == 1:
return n
else:
return (n*power(n, e-1))
n = 4
p = 2
print(power(n, p))
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved