Python Glossary Русский

all Python 3Arithmetic & operatorsfunction

pow()

numbers

Built-in exponentiation function. With two arguments it is the same as **; with three (base, exp, mod) it does modular exponentiation, which is efficient for large numbers.

Syntax

pow(base, exp[, mod])

The three-argument form is not just shorthand: pow(a, b, m) reduces modulo m at every step and never materialises the huge intermediate value, while (a ** b) % m computes the full power first and will hang on a large exponent. It works with ints only, and a negative exponent there is allowed only since Python 3.8, where it yields a modular inverse. With two arguments, pow(2, -1) gives the float 0.5, not an int.

Examples

print(pow(2, 10))  # → 1024
print(pow(3, 3))  # → 27
print(pow(2, -1))  # → 0.5 (отрицательный показатель)
print(pow(2, 10, 1000))  # → 24 (2**10 % 1000, модульная арифметика)
print(pow(3, 100, 7))  # → 4 (быстрое вычисление по модулю)
print(pow(4, 0.5))  # → 2.0 (дробная степень — корень)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy