In Python, calculating the power of a number means raising a base number to a specified exponent. The built-in pow() function makes this operation simple by taking the base and exponent as arguments and returning the calculated result. For example, pow(2, 3) returns 8 because 2^3 = 8. Python also supports a third argument in the pow() function to perform modular exponentiation. In this tutorial, you will learn how to write a Python program to calculate the power of a number using the pow() method, along with its syntax, logic, code, and output.
What Is the pow() Function in Python?
The pow() function in Python is a built-in function used to calculate the power of a number. It raises the base value to the specified exponent.
In simple terms, pow(x, y) calculates x raised to the power y, where x represents the base and y represents the exponent.
Syntax:
pow(base, exponent)
Parameters:
- base: The number that is raised to a power.
- exponent: The value that specifies the power to which the base is raised.
Example:
result = pow(2, 3)
print(result)
Output:
8
Explanation:
The expression pow(2, 3) calculates 2³, which means:
2 × 2 × 2 = 8
Therefore, the output is 8.
What Is the Problem Statement?
Write a Python program to calculate the power of a number using the built-in pow() function.
The program should:
- Accept a base number from the user.
- Accept an exponent from the user.
- Calculate the power using the
pow()function. - Display the calculated result.
Example 1:
Input:
Enter the base: 2
Enter the exponent: 3
Output:
2 raised to the power 3 = 8
Example 2:
Input:
Enter the base: 5
Enter the exponent: 2
Output:
5 raised to the power 2 = 25
How Does the Program Work?
The program follows these simple steps:
- Start the program.
- Read the base number using the
input()function. - Read the exponent from the user.
- Convert both inputs into integers using
int(). - Pass the base and exponent to the
pow()function. - Store the calculated result in a variable.
- Display the result using the
print()function.
Python Program to Calculate Power Using pow() Function
Program:
# Calculate the power of a number using pow()
base = int(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
result = pow(base, exponent)
print(base, "raised to the power", exponent, "=", result)
Output:
Enter the base: 5
Enter the exponent: 4
5 raised to the power 4 = 625
Explanation:
input()accepts the base and exponent from the user.int()converts the entered values into integers.pow(base, exponent)calculates the power of the given base.- The
resultvariable stores the calculated value. print()displays the final result.
For the given input, the base is 5 and the exponent is 4.
Therefore:
5 × 5 × 5 × 5 = 625
Hence, the output is 625.
How to Calculate Power Using pow() With Three Arguments?
Python also supports a three-argument version of the pow() function.
Syntax:
pow(base, exponent, modulus)
It calculates the power and returns the remainder after dividing the result by the modulus.
Mathematically:
pow(x, y, z) = (x^y) % z
Example:
result = pow(2, 3, 3)print(result)
Output:
2
Explanation:
- Base = 2
- Exponent = 3
- Modulus = 3
First, calculate:
2³ = 8
Then calculate the remainder:
8 % 3 = 2
Therefore, the result is 2.
The three-argument form is useful for modular exponentiation, including certain cryptographic calculations. Its arguments must be integers, and the modulus must not be zero.
What Happens When the Exponent Is Negative?
The pow() function also supports negative exponents when using its two-argument form.
A negative exponent calculates the reciprocal of the corresponding positive power.
Example:
result = pow(2, -3)print(result)
Output:
0.125
Explanation:
2⁻³ = 1 / 2³ = 1 / 8 = 0.125
Note: If the base is zero and the exponent is negative, Python raises a ZeroDivisionError.