Python pow() function calculates the power of a number, returning the base elevated to the exponent.
Syntax
pow(base, exponent, modulus)
Parameters
- base: This is the number that will be raised to a certain power.
- exponent: This parameter signifies the power to which the base will be raised.
- modulus(optional): This parameter introduces the modulus operation into the calculation. After computing the base raised to the exponent, the result is then divided by the modulus, and the remainder of this division is returned.
Return value
- If only two arguments are provided, then returns the value of the base raised to the exponent.
- If three arguments are provided, then it returns the value of base raised to the exponent, modulo modulus.
Example 1: Using with two arguments
# Positive base (a), positive exponent (b)
print(pow(3, 4))
# Negative base (a), positive exponent (b)
print(pow(-3, 4))
# Positive base (a), negative exponent (b)
print(pow(3, -4))
# Negative base (a), negative exponent (b)
print(pow(-3, -4))
Output
81
81
0.012345679012345678
0.012345679012345678
Example 2: Using with three arguments
# Base (3) raised to the exponent (4) and then modulo 10
print(pow(3, 4, 10))
So, it does first 3*3*3*3, which is 81, and then 81%10, which is 1. So, that is why it will give us output 1.
1
Example 3: Calculating the floating-point values
# 2.5 (float base) raised to the power of 3 (integer exponent)
print(pow(2.5, 3))
# 2 (integer base) raised to the power of 3.5 (float exponent)
print(pow(2, 3.5))
# 2.5 (float base) raised to the power of 3.5 (float exponent)
print(pow(2.5, 3.5))
Output
15.625
11.313708498984761
24.705294220065465
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.