Write a Python Program to Compute the Power of a Number

In Python, we can easily compute the power of a number using the built-in function pow() or the ** operator.

The pow() function takes two arguments, the base number and the exponent, and returns the result of raising the base to the power of the exponent.

Similarly, the ** operator also takes the base and exponent as operands and returns the same result.

Here’s an example program that computes the power of a number using both pow() and **:

# Python program to compute the power of a number

base = float(input("Enter the base number: "))
exponent = float(input("Enter the exponent: "))

# Using the pow() function
result1 = pow(base, exponent)

# Using the ** operator
result2 = base ** exponent

print(f"{base} raised to the power of {exponent} is equal to {result1} (using pow())")
print(f"{base} raised to the power of {exponent} is equal to {result2} (using **)")

In this program, we first prompt the user to enter the base number and exponent using the input() function.

Then, we use the pow() function to compute the power and store the result in the variable result1.

Similarly, we use the ** operator to compute the power and store the result in the variable result2.

Finally, we print both results using formatted string literals.

Here’s an example output of the program:

Enter the base number: 2
Enter the exponent: 3
2.0 raised to the power of 3.0 is equal to 8.0 (using pow())
2.0 raised to the power of 3.0 is equal to 8.0 (using **)

As you can see, the program correctly computes the power of the number 2 raised to the exponent 3 using both the pow() function and the ** operator.

You can use this program to compute the power of any number with any exponent.