Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python pow() Function and the ** Operator

How Python's pow() function and the ** operator handle exponentiation, including the three-argument modular form, return types, and when to use each.

~4 min readPython
#pow#exponentiation#modular arithmetic#Python built-in
Diagram showing the pow() function and ** operator for exponentiation in Python.

Python gives you two primary ways to compute exponentiation: the ** operator and the built-in pow() function. They look similar and often return the same result, but pow() has an additional three-argument form for modular exponentiation. This article explains how both work, where they differ, and when the modular form is worth using.

How the ** Operator Works

The ** operator is a binary operator that returns the result of raising its left operand to the power of its right operand. It works with integers, floats, and complex numbers:

python
print(2 ** 10) # 1024 print(2.5 ** 3) # 15.625 print(1j ** 2) # (-1+0j)

At the Python level, x ** y dispatches to x.__pow__(y), or to y.__rpow__(x) when the left operand does not support the operation. For integer operands, a non-negative exponent produces an integer and a negative exponent produces a float. Floating-point results follow Python's normal numeric promotion rules.

The pow() Function: Syntax and Parameters

The built-in pow() can be called with two or three arguments. In the two-argument form, it behaves like ** for the common cases:

python
pow(2, 10) # 1024 pow(2, -1) # 0.5

The three-argument form computes (base ** exp) % mod without first creating the full base ** exp value. The arguments must be integers, the exponent must be non-negative for ordinary modular exponentiation, and the modulus must be positive.

python
pow(2, 10, 1000) # 24, because 1024 % 1000 = 24

An exception is the modular inverse case introduced in Python 3.8. If the exponent is negative and the base has an inverse modulo the modulus, pow(base, exp, mod) returns that inverse instead of raising an error. For example, pow(2, -1, 5) returns 3 because 2 * 3 is congruent to 1 modulo 5. If no inverse exists, pow() raises ValueError.

The three-argument result is mathematically the same as (base ** exp) % mod for ordinary non-negative exponents; the advantage is that Python computes it without holding the huge intermediate integer.

Why pow() with Three Arguments Is More Efficient

A direct base ** exp % mod expression first computes the full exponentiation. For large exponents, that intermediate value can be enormous. 2 ** 1000000, for example, has more than 300,000 digits. Storing and reducing such a number is slow and memory-intensive.

The three-argument form uses modular exponentiation: it repeatedly squares and multiplies while reducing the result modulo mod at each step. The numbers stay roughly the size of mod, which makes the calculation practical for large exponents and common in cryptography.

Differences Between pow() and ** for Integer and Float Types

For the two-argument form, pow() and ** return the same types: integer results for non-negative integer exponents and float results for negative integer exponents. The important behavioral differences come from the three-argument form and from how Python exposes the operation.

Feature** operatorpow() function
Two-argument exponentiationYesYes
Three-argument modular formNoYes
Callable objectNoYes
Negative exponent with integer baseReturns floatReturns float
Negative exponent with modulusNo direct equivalentModular inverse (Python 3.8+) when the inverse exists; otherwise ValueError

Because pow() is a function, you can pass it to higher-order callables such as map() or functools.reduce(). The ** operator has no function form.

When to Use pow() with Three Arguments

The three-argument form is important wherever modular arithmetic appears: RSA-style calculations, Diffie-Hellman key exchange, and primality tests such as Miller-Rabin. It is also common in combinatorial problems that need results modulo a prime.

One classic use is computing a modular inverse with Fermat's little theorem. For a prime p and a value a not divisible by p, pow(a, p - 2, p) returns the inverse of a modulo p.

Performance Considerations and Memory Usage

For ordinary exponentiation without a modulus, the ** operator and the two-argument pow() produce the same result and similar performance. Use whichever is clearer. The real performance difference appears when a modulus is involved: prefer the three-argument pow() so Python never creates the full power as an intermediate value.

Floating-point exponentiation is delegated to the C library's pow in CPython. That is normally fine, but if your application needs bit-for-bit reproducible floating-point results across very different C libraries, you should be aware of platform-specific rounding.

Common Pitfalls and Edge Cases

For the ordinary non-negative exponent case, pow(a, b, m) returns the same value as (a ** b) % m; it just computes it more efficiently. For a negative base and a positive modulus, Python's modulo result follows the sign of the divisor, so pow(-2, 3, 5) and ((-2) ** 3) % 5 both return 2.

Useful edge cases include:

  • pow(0, 0) returns 1, matching the usual mathematical convention.
  • pow(0, negative) raises ZeroDivisionError, as does 0 ** negative.
  • With a third modulus argument and a negative exponent, Python 3.8+ attempts to return a modular inverse and raises ValueError if no inverse exists.

Practical Example: Simplified RSA Encryption

To illustrate the value of the modular form, here is a small RSA-like calculation:

python
p = 61 q = 53 n = p * q e = 17 message = 42 ciphertext = pow(message, e, n) print(ciphertext) # 2557

If you instead wrote (message ** e) % n, Python would first compute 42 ** 17, which is small enough here but would be impractical for full-size RSA keys with 2048-bit moduli. Using pow(message, e, n) keeps the intermediate values small.

Choosing Between pow() and **

For simple exponentiation, either form works. ** is idiomatic and readable when you do not need a modulus. Use pow() when you need the three-argument modular form, or when you want to pass the function itself to another callable. There is no general performance reason to prefer pow() over ** for two-argument exponentiation; the modular form is where pow() earns its place.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.