pow()

Raise to a power — with an optional third argument for efficient modular exponentiation used in cryptography and modular arithmetic.

Built-in functionPython 1.0+Live demo
Common call
pow(2, 10) # 1024
Returns
int for integer args (unless negative exp); float when floats involved
Replaces
base**exp — but `pow(a, b, m)` is far faster than `a**b % m` for large exponents
Watch out
0**negative raises; negative exp on int returns float; 3-arg needs integers
pow(basebaseThe base.type: int | float · required, exp[, mod])
int | float

Demo

Live evaluation
Try:
Inputs
basefloatthe base
expfloatthe exponent
modintoptional modulus
Output
pow(2, 10)
1024

pow(a, b) is the same as a**b. The three-argument pow(a, b, m) computes a**b mod m without ever materializing the intermediate a**b — critical when the exponent is large (cryptography, hashing). Negative exponents on integers return floats in the two-arg form; the three-arg form supports negative exp too (3.8+), returning the modular inverse.

Parameters

NameTypeRequiredDescription
baseint | floatyesThe base.
expint | floatyesThe exponent. Negative on integers returns a float unless mod is given.
modintno (None)When given, the result is base**exp mod mod, computed efficiently. All three must be integers; negative exp requires base coprime with mod (3.8+, gives modular inverse).

Return value

int | floatbase to the power of exp. If mod is given, the result is taken modulo mod — computed by fast modular exponentiation without materializing base**exp.

Common patterns

Modular exponentiation for crypto
RSA and Diffie-Hellman both boil down to this call.
ciphertext = pow(plaintext, e, n)
Square root via fractional exponent
x ** 0.5 is the same as sqrt(x) — pow is fine for non-negative x.
root = pow(x, 0.5)
Modular inverse
Since 3.8, three-arg pow with negative exponent gives the modular inverse.
inv = pow(a, -1, m)   # a × inv ≡ 1 (mod m)
Powers of two
Common enough that 1 << n or 2**n often reads as well as pow(2, n).
byte_max = pow(2, 8) - 1

Examples

1. Basic power
pow(2, 10)
Returns
1024
2. Modular (3-arg)
pow(5, 55, 13)
Returns
8
3. Big modular exp
pow(3, 1000, 97)
Returns
36
4. Negative exp → float
pow(2, -3)
Returns
0.125
5. Float exp
pow(4, 0.5)
Returns
2.0
6. 0**0 is 1
pow(0, 0)
Returns
1
7. 0**-1 raises
pow(0, -1)
Returns
ZeroDivisionError: 0.0 cannot be raised to a negative power

Pitfalls

1. `pow(a, b, m)` vs `a**b % m` — same result, wildly different speed
The two-arg + modulus form materializes a**b, which can be astronomically large. The three-arg form uses square-and-multiply and stays within bounds.
Slow / OOM
pow(3, 10**6) % 97
materializes a giant integer first
Fast path
pow(3, 10**6, 97)
stays small; nearly instant
2. Negative exp on ints returns a float (two-arg)
Because the true result is a fraction. If you need modular inverse, use the three-arg form (3.8+).
Float leak
pow(2, -3)
0.125 # not an int
Modular inverse
pow(2, -1, 13)   # 7 — the inverse of 2 mod 13
7
3. Three-arg pow requires INTEGERS
A float base, exp, or mod raises TypeError. Modular exponentiation is defined only on integers.
Type error
pow(2.0, 10, 13)
TypeError: pow() 3rd argument not allowed unless all arguments are integers
Cast first
pow(int(2.0), 10, 13)
10
4. 0**negative raises
Zero to a negative power is 1/0 — undefined. Python raises ZeroDivisionError.
Division by zero
pow(0, -1)
ZeroDivisionError: 0.0 cannot be raised to a negative power
Guard base
pow(0, -1) if base != 0 else default
no crash

When to use

Use it
  • Modular arithmetic in cryptography, hashing, algorithms
  • Powers with a computed exponent (readability over **)
  • Modular inverse via `pow(a, -1, m)` — Python 3.8+
  • When you want an explicit function call in a math-heavy pipeline
Reach for something else
  • Simple constant powers → the ** operator is clearer
  • Powers of two → `1 << n` is idiomatic
  • Cases where operator overloading matters (e.g. numpy) → operator preserves broadcasting

Notes

Complexity
Two-arg: O(log exp) using square-and-multiply for ints. Three-arg: O(log exp) with values bounded by mod — dramatically cheaper for large inputs.
Return
int when all args are ints and exp is non-negative (or three-arg); float otherwise
CPython impl
Python/bltinmodule.c :: builtin_pow — dispatches to type-specific implementations
Memory
Two-arg on huge ints can allocate very large integers; three-arg stays within `mod`
Thread-safe
Yes — a pure computation

FAQ

Python follows the "combinatorial" convention: 0**0 = 1. This matches how empty products and Taylor series expansions are defined, and how most math libraries behave. It is a choice — some mathematicians prefer "undefined" — but Python commits to 1.

History

1.0
pow() has been a builtin since Python 1.0, including the three-argument modular form.
3.0
True division rules apply; negative-exponent ints return floats (unless three-arg).
3.8
Three-arg pow accepts negative exponents — computes the modular inverse when base is coprime with mod.