Raises a number to a power
Usage
The pow(x, y)
function calculates the value of x to the power of y ( x y ).
If a third argument z is specified, pow(x, y, z)
function returns x to the power of y, modulus z ( x y % z ).
Syntax
pow(x,y,z)
Parameter | Condition | Description |
x | Required | The base |
y | Required | The exponent |
z | Optional | The modulus |
pow(x, y)
If two arguments are specified, the pow(x, y)
method returns x to the power of y ( x y ).
# Raise 5 to the power of 2
x = pow(5, 2)
print(x)
# Prints 25
# negative number
x = pow(-2, 3)
print(x)
# Prints -8
# float
x = pow(2.5, 2)
print(x)
# Prints 25
# complex number
x = pow(3+4j, 2)
print(x)
# Prints (-7+24j)
# Negative exponent
x = pow(2, -2)
print(x)
# Prints 0.25
If the exponent (second argument) is negative, the method returns float result.
You can achieve the same result using the power operator **
.
x = 10**2
print(x)
# Prints 100
pow(x, y, z)
If all the three arguments are specified, pow(x, y, z)
function returns x to the power of y, modulus z ( x y % z ).
# Return the value of 5 to the power of 2, modulus 3
x = pow(5, 2, 3)
print(x)
# Prints 1
You can achieve the same result using the power operator **
and modulus operator %
.
x = 5**2%3
print(x)
# Prints 1
If z is present, x and y must be of integer types, and y must be non-negative. Otherwise, the function raises exception.