Python pow() Function

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)

Python pow() function parameters
ParameterConditionDescription
xRequiredThe base
yRequiredThe exponent
zOptionalThe modulus

pow(x, y)

If two arguments are specified, the pow(x, y) method returns x to the power of y ( x y ).

Example: Raise 5 to the power of 2

x = pow(5, 2)
print(x)	# 25

More Examples:

# negative number
x = pow(-2, 3)
print(x)	# -8
# float
x = pow(2.5, 2)
print(x)	# 25
# complex number
x = pow(3+4j, 2)
print(x)	# (-7+24j)
# Negative exponent 
x = pow(2, -2)
print(x)	# 0.25

If the exponent (second argument) is negative, the method returns float result.

You can achieve the same result using the power operator **.

Example:

x = 10**2
print(x)	# 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 ).

Example: Return the value of 5 to the power of 2, modulus 3

x = pow(5, 2, 3)
print(x)	# 1

You can achieve the same result using the power operator ** and modulus operator %.

Example:

x = 5**2%3
print(x)	# 1

If z is present, x and y must be of integer types, and y must be non-negative. Otherwise, the function raises exception.