Python Program to Calculate the Power of a Number
In this tutorial, we will discuss a Python program to the power of a number.
Before going to the program first, let us understand how the Power of a Number is calculated.
Power of a Number:
- The power of a number is calculated as baseexponent\text{base}^\text{exponent}.
Related: Python Program to Find the Median of a List
Program code to calculate the power of a number in Python
# Calculate the Power of a Number in Python def calculate_power(base, exponent): return base ** exponent base = float(input("Enter the base: ")) exponent = int(input("Enter the exponent: ")) print(f"{base} raised to the power of {exponent} is {calculate_power(base, exponent)}.")
Explanation
- Function Definition: The
calculate_power
function takes two parameters:base
andexponent
, and returns the result of raising the base to the power of the exponent. - Main Program: The program prompts the user to enter the base and exponent, then calculates the power using the
calculate_power
function and prints the result.
Output
- When you run the above program, it will prompt you to enter the base and exponent.
- After entering the values, it will calculate the power and print the result.
Conclusion
- In this tutorial, we learned how to calculate the power of a number using a Python program.
- Understanding this concept is essential for solving various mathematical problems and enhancing your programming skills.