The pow() function in Python is a built-in function used to calculate the power of a number. It raises a number (called the base) to the power of another number (called the exponent).
In simple terms, pow(a, b) calculates:
[ a^b ]
The function can also take a third argument for modular exponentiation, which is commonly used in cryptography and number theory.
Syntax 1: Two arguments
pow(base, exponent)
Syntax 2: Three arguments
pow(base, exponent, modulus)
base The number to be raised to a power.
exponent The power to which the base is raised.
modulus (optional) A number used to compute the remainder after exponentiation.
[ (base^{exponent}) \mod modulus ]
print(pow(2, 3))
Output
8
Explanation
[ 2^3 = 8 ]
print(pow(5, 4))
Output
625
Explanation
[ 5^4 = 625 ]
print(pow(4, 0.5))
Output
2.0
Explanation
0.5 calculates its square root.[ 4^{0.5} = \sqrt{4} = 2 ]
print(pow(2, 5, 3))
Output
2
Explanation
First compute the power:
[ 2^5 = 32 ]
Then apply modulus:
[ 32 \mod 3 = 2 ]
pow() and ** Operator| Feature | pow() |
** Operator |
|---|---|---|
| Purpose | Power calculation | Power calculation |
| Syntax | pow(a, b) |
a ** b |
| Modulus support | Yes (pow(a, b, m)) |
No |
Example:
print(pow(3, 2))
print(3 ** 2)
Output:
9
9
side = 5
area = pow(side, 2)
print("Area of square:", area)
Output
Area of square: 25
Explanation
[ side^2 ]
pow() is a built-in Python function used to calculate powers of numbers.** power operator, but offers an extra modulus parameter.