Python Program to Compute the Power of a Number

Power:

A power is an expression that describes repeated multiplication of the same factor.

Examples:

Example1:

Positive Exponent:

Input:

base = 7 power = 4

Output:

The result is 2401

Example2:

Negative Exponent:

Input:

base = 7 power = -2

Output:

The result is 0.02040816326530612

This article will teach you how to compute the power of a number.

Calculate the Power of a Number

Below are the ways to calculate the power of a number :

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using While loop

  • Scan the base and power values.
  • Create a variable resultValue and initialize it with 1.
  • Using the while loop, we continue to multiply the resultValue by the base until the exponent reaches zero.
  • Print the resultValue.

Below is the implementation:

# given base value and power value
baseValue = 7
powerValue = 4
# initialize the result value to 1
resultValue = 1
# Loop till power becomes 0
while powerValue != 0:
    resultValue = resultValue * baseValue
    powerValue = powerValue-1

print("The result is", resultValue)

Output:

The result is 2401

Method #2: Using For loop

  • Instead of a while loop, we’ve used a for loop here.
  • Scan the base and power values.
  • Create a variable resultValue and initialize it with 1.
  • The exponent is decremented by one for each iteration, and the resultValue is multiplied by the base exponent a number of times.
  • Print the resultValue.

Below is the implementation:

# given base value and power value
baseValue = 7
powerValue = 4
# initialize the result value to 1
resultValue = 1
# Loop till power becomes 0 using for loop
for i in range(powerValue):
    resultValue = resultValue*baseValue
    # decrement the powerValue
    powerValue = powerValue-1

print("The result is", resultValue)

Output:

The result is 2401

Note:

If you have a negative exponent, none of the above methods will function.
You must use the pow() function from the Python library to accomplish this.

Method #3:Using pow() function

We can calculate the power value using pow() function.

It works for negative exponent too.

1)Calculating the power of positive exponent

Below is the implementation:

# given base value and power value
baseValue = 7
powerValue = 4
# calculating the power of base
resultValue = pow(baseValue, powerValue)
# print the result
print("The result is", resultValue)

Output:

The result is 2401

2)Calculating the power of negative exponent

Below is the implementation:

# given base value and power value
baseValue = 7
powerValue = -2
# calculating the power of base
resultValue = pow(baseValue, powerValue)
# print the result
print("The result is", resultValue)

Output:

The result is 0.02040816326530612

Method #4:Using ** operator in Python

We can calculate the power value using ** .

It works for negative exponent too.

1)Calculating the power of positive exponent

Below is the implementation:

# given base value and power value
baseValue = 7
powerValue = 4
# calculating the power of base
resultValue = baseValue**powerValue
# print the result
print("The result is", resultValue)

Output:

The result is 2401

2)Calculating the power of negative exponent

Below is the implementation:

# given base value and power value
baseValue = 7
powerValue = -2
# calculating the power of base
resultValue = baseValue**powerValue
# print the result
print("The result is", resultValue)

Output:

The result is 0.02040816326530612

Related Programs: