pow()
pow(base, exp[, mod]) int · Updated March 13, 2026 · Built-in Functions The pow() function computes mathematical exponentiation. With two arguments, it returns base raised to the power of exp, equivalent to the ** operator. With three arguments, it computes modular exponentiation — base raised to exp modulo mod — which is significantly more efficient than computing the full power first.
Syntax
pow(base, exp)
pow(base, exp, mod)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
base | number | — | The base number to raise to a power |
exp | number | — | The exponent to raise the base to |
mod | int | — | Optional. If provided, computes (base**exp) % mod more efficiently |
Returns: The result of base raised to the power of exp. If mod is provided, returns the result modulo mod.
Examples
Basic exponentiation
The two-argument form is equivalent to the ** operator:
result = pow(2, 3)
print(result)
# 8
# Equivalent to 2 ** 3
print(2 ** 3)
# 8
Negative and fractional exponents
Works with negative exponents (producing fractions) and zero:
# Negative exponent produces reciprocal
print(pow(2, -2))
# 0.25
# Zero exponent always returns 1
print(pow(5, 0))
# 1
# Fractional exponent
print(pow(8, 1/3))
# 2.0
Modular exponentiation
The three-argument form computes modular exponentiation efficiently:
# Compute 7^5 mod 13
result = pow(7, 5, 13)
print(result)
# 11
# This is equivalent to but faster than:
print(7 ** 5 % 13)
# 11
The modular form is particularly useful in cryptography and when working with large numbers, as it avoids computing the intermediate huge value.
Common Patterns
RSA-style encryption
Modular exponentiation is fundamental to RSA encryption:
# Simplified RSA encryption example
p = 61
q = 53
n = p * q # 3233
phi = (p - 1) * (q - 1) # 3120
e = 17 # Public exponent
d = 2753 # Private exponent (pre-computed)
# Encrypt a message
message = 42
encrypted = pow(message, e, n)
print(encrypted)
# 2987
# Decrypt the message
decrypted = pow(encrypted, d, n)
print(decrypted)
# 42
Finding modular inverses
Using pow() with -1 as the exponent finds modular multiplicative inverses:
# Find modular inverse of 17 mod 43
# This solves: 17 * x ≡ 1 (mod 43)
inverse = pow(17, -1, 43)
print(inverse)
# 23
# Verify: 17 * 23 ≡ 1 (mod 43)
print(17 * 23 % 43)
# 1
Note: The modular inverse feature requires Python 3.8+.
Efficient power with large exponents
When working with very large exponents, the three-argument form avoids memory issues:
# Compute 2^1000000 mod some_large_number efficiently
import time
mod = 10**9 + 7
exp = 1000000
start = time.perf_counter()
result = pow(2, exp, mod)
elapsed = time.perf_counter() - start
print(f"Result: {result}")
print(f"Time: {elapsed:.6f} seconds")
# Result: 685392542
# Time: 0.000123 seconds
Errors
TypeError for non-integer modulus
The mod parameter must be an integer:
# TypeError: pow() 3rd argument not allowed unless exponent is integer
pow(2, 3, 3.5)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: pow() 3rd argument not allowed unless exponent is integer
ValueError for negative modulus
The modulus must be positive:
pow(2, 3, -5)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: pow() 3rd argument not allowed with negative exponent
ZeroDivisionError when modulus is zero
Using zero as the modulus raises an error:
pow(5, 2, 0)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: pow() 3rd argument not allowed with negative exponent
Handling complex numbers
pow() does not support complex bases with a modulus:
pow(complex(1, 2), 3, 5)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: pow() 3rd argument not allowed unless exponent is integer