Python pow() Function
pow(base, exp, mod=None) returns base raised to the power exp; with mod it returns (base ** exp) % mod computed efficiently.For everyday exponents, pow(2, 10) and 2 ** 10 are the same. The function earns its place because of its optional third argument, which does modular exponentiation quickly even for huge numbers, a technique used in checksums and cryptography.
It handles integers, floats and (without mod) complex numbers. See the lesson on numbers for how ints and floats differ, since the result type depends on the inputs.
| Syntax | pow(base, exp, mod=None) |
|---|---|
| Parameters | base: number; exp: exponent; mod: optional modulus (integers only) |
| Returns | base ** exp, or base ** exp % mod when mod is given |
| Result type | int for int inputs with a non-negative exponent; float for a negative exponent |
| Raises | ValueError if mod is 0; TypeError for a float with mod |
How pow(base, exp) compares with the ** operator
With two arguments, pow(a, b) and a ** b give the same value. A negative exponent produces a float: pow(2, -2) is 0.25. A fractional exponent is a root: pow(9, 0.5) is 3.0. Prefer ** in plain arithmetic and pow() when you need the modulus or want to pass the operation as a function, for example to map().
print(pow(2, 10))
print(2 ** 10)
print(pow(2, -2))
print(pow(9, 0.5))Three-argument pow(base, exp, mod)
pow(a, b, m) gives (a ** b) % m but without creating the enormous intermediate number, so pow(7, 1000000, 13) is instant. All three arguments must be integers, and mod cannot be zero. Since Python 3.8 the exponent may be negative when base and mod are coprime: the result is then a modular inverse. For example pow(3, -1, 7) is 5, because 3 * 5 leaves remainder 1 when divided by 7.
print(pow(2, 10, 1000))
print(pow(3, -1, 7))pow() vs ** vs math.pow()
| Tool | Supports mod? | Return for `2, 3` | Notes |
|---|---|---|---|
pow(2, 3) | Yes | 8 (int) | Built in, keeps ints exact |
2 ** 3 | No | 8 (int) | Operator form, same as two-argument pow() |
math.pow(2, 3) | No | 8.0 (float) | Always floats; needs import math |
pow(2, 3, 5) | Yes | 3 | 8 % 5 |
Examples you can run
Press Run, then edit the code and run it again.
Squares, cubes and roots
The exponent can be any number.
print(pow(5, 2))
print(pow(2, 3))
print(pow(16, 0.5))
print(pow(2, -3))Expected output25
8
4.0
0.125
Compound interest
Money grows by (1 + rate) ** years.
principal = 1000
rate = 0.10
years = 2
amount = principal * pow(1 + rate, years)
print(round(amount, 2))Expected output1210.0
Modular exponentiation
pow(2, 10, 1000) is the last three digits of 1024.
print(pow(2, 10))
print(pow(2, 10, 1000))
print(pow(2, 3, 5))Expected output1024
24
3
Modular inverse
A negative exponent with mod finds the inverse (Python 3.8+).
inv = pow(3, -1, 7)
print(inv)
print(3 * inv % 7)Expected output5
1
Common mistakes
Using a float with the modulus
✗ Wrong
print(pow(2, 3.5, 5))✓ Right
print(pow(2, 3, 5))With mod, every argument must be an int.
A zero modulus
✗ Wrong
print(pow(2, 3, 0))✓ Right
print(pow(2, 3, 5))Use a non-zero modulus.
Zero to a negative power
✗ Wrong
print(pow(0, -1))✓ Right
base = 0
print(pow(base, -1) if base else "undefined")0 ** -1 would be 1 / 0. Guard against a zero base.
Expecting an int from a negative exponent
✗ Wrong
print(pow(2, -2))✓ Right
print(1 / pow(2, 2))Negative exponents give floats. Use 1 / pow(2, 2) for the same meaning or convert deliberately.
Python pow() Function: questions people ask
What is the difference between pow() and ** in Python?
For two arguments there is no difference in result. pow() also accepts a third argument for modular arithmetic, which ** cannot do.
What does the third argument of pow() do?
It computes (base ** exp) % mod efficiently. For instance pow(2, 10, 1000) returns 24.
What is the difference between pow() and math.pow()?
math.pow() always returns a float and has no modulus option, while built-in pow() keeps integers exact and supports mod.
Keep learning
Step-by-step lessons:
Python NumbersPython OperatorsPython math and statistics
Related:
Python abs() FunctionPython divmod() FunctionPython round() FunctionPython sum() Function
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →