Built-in function

pow()

Raises a number to a power, like the ** operator, and with a third argument finds the remainder of that power quickly (modular power).

BeginnerEvery example below runs in your browser. Change the code and press Run.

pow(base, exp, mod=None)

pow(base, exp) multiplies base by itself exp times. pow(2, 3) is 2 * 2 * 2, which is 8. It gives exactly the same answer as the operator base ** exp.

Its special power is the third argument. pow(base, exp, mod) gives the remainder of base ** exp divided by mod, but without ever building the huge number in between. This is how cryptography and hash code work with numbers hundreds of digits long.

Negative and fractional powers work too: pow(2, -1) is 0.5 and pow(9, 0.5) is 3.0, the square root. Watch out: without the third argument, a negative power always gives a float, even when both inputs are whole numbers.

Parameters

ParameterWhat it is
base
required
The number to multiply by itself.
exp
required
The power: how many times to multiply. It can be negative or a fraction.
mod
optional, default None
If given, the result is (base ** exp) % mod, computed in a fast way. All three numbers must then be whole numbers.

Return value

A number. Whole number inputs with a non-negative power give an int; a negative power or a float input gives a float. With mod, the result is always an int.

Examples

1Powers and the ** operator

Pythoneditable
print(pow(2, 10))
print(2 ** 10)
print(pow(5, 0))
print(pow(1.5, 2))
Output
1024
1024
1
2.25

2Negative and fractional powers

Pythoneditable
print(pow(2, -2))
print(pow(16, 0.5))
print(pow(27, 1/3))
Output
0.25
4.0
3.0

A power of 0.5 is a square root, 1/3 a cube root.

3Compound interest

Pythoneditable
deposit = 1000
rate = 0.03
for years in [1, 5, 10]:
    value = deposit * pow(1 + rate, years)
    print(f"After {years:>2} years: ${value:,.2f}")
Output
After  1 years: $1,030.00
After  5 years: $1,159.27
After 10 years: $1,343.92

4Modular power: the last digits of a giant number

2 ** 1000 has 302 digits. With a third argument, pow() finds its last three digits without building it.

Pythoneditable
print(pow(7, 222, 10))
print(pow(2, 1000, 1000))
print(pow(3, 4, 5))
Output
9
376
1

pow(3, 4, 5) is 81 % 5, which is 1.

5Modular inverse (Python 3.8 and later)

Pythoneditable
inverse = pow(3, -1, 7)
print(inverse)
print(3 * inverse % 7)
Output
5
1

With a modulus, a power of -1 finds the number that multiplies with 3 to give remainder 1. It is used in cryptography.

Common mistakes

Expecting -2 ** 2 to be 4

The ** operator binds tighter than the minus sign, so -2 ** 2 means -(2 ** 2). Use brackets, or pow(-2, 2).

Pythoneditable
print(-2 ** 2)
print((-2) ** 2)
print(pow(-2, 2))
Output
-4
4
4

Using ^ for powers

In Python ^ is not a power: it is bitwise XOR. 2 ^ 3 gives 1. Use ** or pow().

Pythoneditable
print(2 ^ 3)
print(2 ** 3)
Output
1
8

Questions people ask

What is the difference between pow() and math.pow()?

math.pow() always converts to float, so math.pow(2, 3) is 8.0 and very big results lose precision. The built-in pow() keeps whole numbers exact and supports the third mod argument.

How do I calculate a square root in Python?

pow(x, 0.5) or x ** 0.5 works. math.sqrt(x) is clearer to read, and math.isqrt(x) gives the whole-number square root.

Is pow() faster than **?

For two arguments they are the same. With three arguments, pow(a, b, m) is far faster than a ** b % m for big numbers, because it never builds a ** b.

Spotted a mistake on this page? Tell us