Find Power of a Number in Python
In this post, you will learn how to find the power of a number using the Python programming language.
The power (or exponent) of a number says how many times to use the number in a multiplication. The power defines a base number raised to the exponent, where the base number is the factor which is multiplied by itself, and the exponent denotes the number of times the same base number is multiplied.
Find the power of a number in Python using for loop
In the given Python program, we are calculating the power of a given number using the for loop. In each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times.
base = 5
exponent = 4
result = 1
print("Value of base: ", base);
print("Value of exponent: ", exponent);
for exponent in range(exponent, 0, -1):
result *= base
print("Output: " + str(result))
Output of the above code:
Value of base: 5
Value of exponent: 4
Output: 625
Power of a Number in Python using while loop
In the given Python program, we are calculating the power of a given number using the while loop. In this, we keep on multiplying the result by base until the exponent becomes zero.
base = 6
exponent = 3
result = 1
print("Value of base: ", base);
print("Value of exponent: ", exponent);
while exponent != 0:
result *= base
exponent-=1
print("Output: " + str(result))
Output of the above code:
Value of base: 6
Value of exponent: 3
Output: 216
Power of a Number in Python using pow() function
The predefined method pow() is also used to find out the power of a number. Before using this method, we need to import the math module. This function takes two arguments as input, first is the number itself, and the second one is the power of that number.
import math
base = float(input("Enter the base value: "))
exponent = float(input("Enter the exponent value: "))
sqrt = math.pow(base, exponent)
print("Output: ", sqrt)
Output of the above code:
Enter the base value: 2
Enter the exponent value: 3
Output: 8.0
Enter the base value: 4.2
Enter the exponent value: 3
Output: 74.08800000000001
Power of a Number in Python using recursion
The following Python program calculates the base power exponent using a recursive function. In this program, we read the value of base and exponent from the user using the input() function, and then we calculate baseexponent using a recursive function power_num().
The base condition is given that if the exponential power is equal to 1, the base number is returned. If the exponential power isn't equal to 1, the base number multiplied with the power function is called recursively with the arguments as the base and power minus 1.
# Python power of a number using recursion
# Recursive function
def power_num(x,num):
# if power is 0 then return 1
if num==0:
return 1
# if power is 1 then number is
# returned
elif num==1:
return(x)
else:
return x * power_num(x, num-1)
# Reading inputs base and exponent
base = float(input("Enter value of base: "))
exponent = int(input("Enter value of exponent: "))
# Function call
result = power_num(base, exponent)
# Displaying output
print("Output : ", result)
Output of the above code:
Enter value of base: 4
Enter value of exponent: 3
Output : 64.0
Enter value of base: 7
Enter value of exponent: 0
Output : 1
Enter value of base: -2
Enter value of exponent: 3
Output : -8.0
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary