Python pow() function computes the power of a number by raising the first argument to the second argument. This function returns the value of x to the power of y (xy).
pow(x, y, z)
- x: It is a number, a base
- y: It is a number, an exponent.
- z (optional): It is a number and the modulus.
Python pow() function example
A simple example code finds the value of a number raised to a certain power.
# returns 2^2
print(pow(2, 3))
# returns -2^3
print(pow(-2, 3))
# returns 1/2^3
print(pow(2, -3))
# returns -1/-2^3
print(pow(-2, -3))
Output:
pow() with Modulus
Let’s calculate the value of 4 to the power of 3, modulus 5 (same as (4 * 4 * 4) % 5):
x = pow(4, 3, 5)
print(x)
print(type(x))
Output: 4
Comment if you have any doubts or suggestions on this Python function topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.