The pow()
method computes the power of a number by raising the first argument to the second argument
Example
# compute 3^4
print(pow(3, 4));
# Output: 81
pow() Syntax
The syntax of pow()
is:
pow(number, power, modulus [optional])
pow() Parameters
The pow()
method takes three parameters:
- number- the base value that is raised to a certain power
- power - the exponent value that raises number
- modulus - (optional) divides the result of number paused to a power and finds the remainder: number^power% modulus
pow() Return Value
The pow()
method returns:
- number^power - number, raised to a certain power
- number^power % modulus - with the modulus argument
- 1 - if the value of power is 0
- 0 - if the value of number is 0
Example 1: Python pow()
# returns 2^2
print(pow(2, 2))
# returns -2^2
print(pow(-2, 2))
# returns 1/2^2
print(pow(2, -2))
# returns -1/-2^2
print(pow(-2, -2))
Output
4 4 0.25 0.25
The pow()
method helps us find the value of a number raised to a certain power.
In the above example,
pow(2,2)
is 22 - results in 4pow(-2,2)
is -22 - results in -4pow(2,-2)
is 1/22 - results in 0.25pow(-2,-2)
is -1/-22 - results in 0.25
Example 2: pow() with Modulus
x = 7
y = 2
z = 5
# compute x^y % z
print(pow(x, y, z))
Output
4
In the above example, we have raised the number 7 to the power 2 which results in 49.
Since we have used the pow()
method with three arguments x, y, and z, the third argument 5 divides the result of 72 and finds the remainder.
That's why we get the output 4.
Recommended Readings: