In Python, pow(base, exp) returns base raised to the power exp; it gives the same result as base ** exp. A negative exponent produces a float: pow(2, -2) is 0.25.
With a third argument, pow(base, exp, mod) returns (base ** exp) % modwithout building the huge intermediate number, which makes it fast for cryptography and number puzzles. Since Python 3.8, exp may be negative in this form to compute a modular inverse.
pow() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Two arguments for a plain power, three for a power modulo a number.
python
result = pow(base, exp)
remainder = pow(base, exp, mod)
Call
What it does
pow(2, 10)
Returns 1024.
pow(2, -2)
Returns 0.25 (a float).
pow(3, 4, 5)
Returns 1, which is 81 % 5.
pow(3, -1, 7)
Returns 5, the inverse of 3 modulo 7 (Python 3.8+).
Since Python 3.8 you may also pass the arguments by keyword: pow(base=2, exp=5).
Library declarations
python
pow(base, exp, mod=None)
# two args: base ** exp
# three args (ints only): (base ** exp) % mod
Parameters
Parameter
Description
base
Required. The number to raise.
exp
Required. The exponent. May be negative or a float in the two-argument form.
mod
Optional. A non-zero int. When given, all three arguments must be ints.
Return Value
Return Value
Description
int
For int arguments with a non-negative exponent, or the three-argument form.
float
For a negative int exponent or any float argument.
complex
For a negative base with a fractional exponent, such as pow(-8, 1/3).
Python Examples
Example 1 — Powers, Negative Exponents, and Roots
Compare pow() with the ** operator for whole, negative, and fractional exponents.
Whole exponent.pow(2, 10) multiplies 2 by itself ten times and stays an exact int, 1024.
Negative exponent.pow(10, -2) means 1 / 10**2, so the result is the float 0.01.
Fractional exponent. An exponent of 0.5 is a square root: pow(9, 0.5) returns 3.0.
Same as **. Anything to the power 0 is 1, and the last line confirms 2 ** 10 equals pow(2, 10).
Example 2 — Find the Last Digits of a Huge Power
Use the three-argument form to get the last digits of numbers too big to print comfortably.
python
fast = pow(7, 222, 100)
slow = (7 ** 222) % 100
print("last two digits of 7**222:", fast)
print("same as ** then %:", fast == slow)
print("last digit of 2**100:", pow(2, 100, 10))
Output
last two digits of 7**222: 49
same as ** then %: True
last digit of 2**100: 6
How It Works
Remainder by 100. The last two digits of any number are its remainder when divided by 100.
No giant number.pow(7, 222, 100) reduces modulo 100 after every step; 7**222 itself has 188 digits.
Same answer. Computing 7 ** 222 first and then % 100 agrees, just more slowly.
Last digit. Powers of 2 end in 2, 4, 8, 6 in a cycle; 100 is a multiple of 4, so 2**100 ends in 6.
Example 3 — Modular Inverse and Common Errors
Compute an inverse with a -1 exponent (Python 3.8+) and see the two errors the three-argument form can raise.
python
inverse = pow(3, -1, 7)
print("inverse of 3 mod 7:", inverse)
print("check:", (3 * inverse) % 7)
try:
pow(2, -1, 4)
except ValueError:
print("2 has no inverse mod 4")
try:
pow(2.0, 3, 5)
except TypeError:
print("three-argument pow() needs integers")
Output
inverse of 3 mod 7: 5
check: 1
2 has no inverse mod 4
three-argument pow() needs integers
How It Works
Inverse means product 1.pow(3, -1, 7) finds the number that multiplied by 3 leaves remainder 1 when divided by 7.
Verified.3 * 5 is 15, and 15 % 7 is 1, so 5 is correct.
No inverse. 2 and 4 share the factor 2, so no inverse exists and ValueError is raised.
Ints only. A float base in the three-argument form raises TypeError.
Frequently Asked Questions
Yes, for two arguments the results are the same. The built-in pow() adds an optional third argument for a fast modulus.
It never builds the full power, so it is much faster and uses little memory when exp is large, which matters in cryptography and math puzzles.
Since Python 3.8 use pow(a, -1, m). It raises ValueError if a and m share a common factor.
math.pow() always converts to float and has no modulus argument. The built-in pow() keeps exact ints and supports three arguments.
A negative base with a fractional exponent has no real float result in general, so Python returns the principal complex root. Use -(8 ** (1/3)) if you want the real cube root.