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.

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:
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:
pow(2, 10) # 1024
pow(2, -1) # 0.5The 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.
pow(2, 10, 1000) # 24, because 1024 % 1000 = 24An 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 | ** operator | pow() function |
|---|---|---|
| Two-argument exponentiation | Yes | Yes |
| Three-argument modular form | No | Yes |
| Callable object | No | Yes |
| Negative exponent with integer base | Returns float | Returns float |
| Negative exponent with modulus | No direct equivalent | Modular 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)returns1, matching the usual mathematical convention.pow(0, negative)raisesZeroDivisionError, as does0 ** negative.- With a third modulus argument and a negative exponent, Python 3.8+ attempts to return a modular inverse and raises
ValueErrorif no inverse exists.
Practical Example: Simplified RSA Encryption
To illustrate the value of the modular form, here is a small RSA-like calculation:
p = 61
q = 53
n = p * q
e = 17
message = 42
ciphertext = pow(message, e, n)
print(ciphertext) # 2557If 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 toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.