pow() is Python’s built-in function for efficient exponentiation — computing base raised to an exponent, with optional modulus for modular exponentiation. It supports integers, floats, and negative exponents (fractional powers), and is significantly faster and more accurate than ** or math.pow() for large exponents or modular cases. In 2026, pow() remains a cornerstone in cryptography (modular exponentiation for RSA, Diffie-Hellman), scientific computing (large powers, modular inverse), data science (feature engineering, polynomial evaluation), and algorithmic work — with built-in support for three-argument modular form (pow(base, exp, mod)) that avoids overflow and is constant-time for large numbers.
Here’s a complete, practical guide to using pow() in Python: basic powering, modular exponentiation, negative/fractional exponents, real-world patterns (earthquake energy scaling, modular inverse, crypto primitives), and modern best practices with type hints, performance, precision, and integration with NumPy/Dask/Polars/mpmath/gmpy2.
Basic pow(base, exp) — integer & float exponentiation.
print(pow(2, 10)) # 1024
print(pow(3, 5)) # 243
print(pow(2.5, 3)) # 15.625 (float)
print(pow(10, -2)) # 0.01 (negative exp ? 1/base**abs(exp))
print(pow(-2, 3)) # -8 (odd exponent preserves sign)
print(pow(-2, 4)) # 16 (even exponent positive)
Modular exponentiation — pow(base, exp, mod) — efficient & overflow-safe.
# Modular exponentiation (fast, no overflow)
print(pow(2, 100, 10007)) # 2¹?? mod 10007 = 2792
# Modular inverse (negative exponent)
print(pow(3, -1, 7)) # 5 (because 3×5 ? 1 mod 7)
# Large exponent with modulus (cryptography staple)
base = 5
exp = 65537
mod = 2**256 - 2**32 - 977 # common in elliptic curve crypto
print(pow(base, exp, mod)) # very large result, computed efficiently
Real-world pattern: earthquake energy scaling & modular checks — using pow for large exponents and modular arithmetic.
import pandas as pd
import numpy as np
df = pd.read_csv('earthquakes.csv')
# Energy ~ 10^(1.5 * mag) — use pow for large exponents
df['energy'] = 10 ** (1.5 * df['mag']) # equivalent to pow(10, 1.5 * df['mag'])
# Modular checksum example (simple integrity check)
def checksum(row):
seed = int(row['time'].timestamp()) % 10007
return pow(int(row['mag'] * 100), 3, 10007) == seed % 10007
df['checksum_valid'] = df.apply(checksum, axis=1)
print(df['checksum_valid'].value_counts())
# Modular inverse for scaling ratios (cryptographic-like)
def scale_ratio(a, b, mod=10**9 + 7):
return (a * pow(b, -1, mod)) % mod
print(scale_ratio(123456789, 987654321)) # modular division
Best practices for pow() in Python & data workflows. Prefer pow(base, exp, mod) — for modular exponentiation (fast, secure, no overflow). Modern tip: use Polars pl.col('base').pow(pl.col('exp')) — vectorized; Dask for distributed pow. Use three-argument form — for RSA, Diffie-Hellman, modular inverse (pow(a, -1, m)). Add type hints — def mod_pow(base: int, exp: int, mod: int) -> int: return pow(base, exp, mod). Use pow() over ** — for large exponents or modular cases (faster, more precise). Handle negative exponents — returns float (1/base**abs(exp)). Use pow() with floats — but prefer math.pow() for pure float (no integer bias). Use pow(base, exp) — for integer results when possible. Use pow() in hashing — pow(hash_val, 31, large_prime). Use pow() in crypto — RSA decryption: pow(cipher, d, n). Use pow() in number theory — Euler totient, Fermat’s little theorem. Profile performance — pow() with modulus is optimized (binary exponentiation). Use pow(base, exp, mod=None) — full form. Use pow(-2, 3) — preserves sign for odd exponents. Use pow(2, 1000000) — handles very large exponents efficiently. Use pow() with decimal.Decimal — for precise decimal powers. Use mpmath.mpf — for arbitrary precision. Use gmpy2.powmod() — faster modular exp for very large numbers.
pow(base, exp[, mod]) computes base raised to exp, optionally modulo mod — efficient for large exponents, modular arithmetic, and modular inverse. In 2026, use three-argument form for crypto & number theory, vectorized in NumPy/Polars/Dask for data, and prefer over ** for performance & precision. Master pow(), and you’ll handle exponentiation cleanly and efficiently in any numerical, cryptographic, or data workflow.
Next time you need to raise a number to a power — use pow(). It’s Python’s cleanest way to say: “Compute this exponentiation — fast, accurate, and modular when needed.”