Python 3 reference · Built-in Functions

Python pow() Function

Raise a number to a power, like base ** exp. With a third argument it computes the result modulo a number efficiently.

Python 3 Since 3.0

Syntax

pow(base, exp, mod=None)

Parameters

ParameterDescription
base The number to raise.
exp The exponent.
mod optional If given, return (base ** exp) % mod, computed efficiently. Default: None

Return value

The result; an int, float or complex depending on the inputs.

Examples

Powers and rootsTry it yourself
print(pow(2, 10), 2 ** 10)
print(pow(2, -1), pow(9, 0.5))

Output

1024 1024
0.5 3.0
Modular arithmeticTry it yourself
print(pow(7, 128, 13))         # fast, no huge number built
print(pow(3, -1, 7))           # modular inverse: 3 * 5 % 7 == 1

Output

3
5

Good to know

The three-argument form is what cryptography and hashing code uses: it never computes the huge intermediate number. A negative exponent with mod gives the modular inverse (Python 3.8+).

Version history

VersionChange
Python 3.0Added
Python 3.8Negative exponents with mod compute modular inverses; arguments can be passed by keyword.