Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

pow()

Computes a power, optionally performing modular exponentiation.

Version: Python 3.14; repository image, production runtime not attested

Syntax

pow(base, exp, mod=None)

Computes a power, optionally performing modular exponentiation.

With an integer modulus a negative exponent can request a modular inverse when the base and modulus are coprime.

With three integer arguments, modular exponentiation computes the residue without first building the full power. Here 1024 leaves remainder 2 on division by 7. The inverse of 3 modulo 7 is 5 because 3×5 leaves remainder 1. A negative exponent requires coprimality and a nonzero modulus.

assert pow(2, 10, 7) == 1024 % 7 == 2
assert pow(3, -1, 7) == 5 and (3 * 5) % 7 == 1

Examples

print(pow(2, 10, 7))
print(pow(3, -1, 7))

Result

2
5

With three integer arguments, modular exponentiation computes the residue without first building the full power. Here 1024 leaves remainder 2 on division by 7. The inverse of 3 modulo 7 is 5 because 3×5 leaves remainder 1. A negative exponent requires coprimality and a nonzero modulus.

Sources