Python Glossary Русский

all Python 3operatorfunction

operator.pow()

arithmetic

The function form of a ** b: exponentiation you can pass to map, reduce or functools.partial.

Syntax

operator.pow(a, b)

Unlike the built-in pow(), there is no third modulus argument: operator.pow(2, 10, 7) raises TypeError, and a fast a ** b % m needs pow(a, b, m). Beware of reduce: reduce(operator.pow, [2, 3, 2]) folds from the left, (2 ** 3) ** 2 = 64, while the expression 2 ** 3 ** 2 groups from the right and gives 512.

Examples

import operator
print(list(map(operator.pow, [2, 3], [10, 2])))  # → [1024, 9]
import functools, operator
print(functools.reduce(operator.pow, [2, 3, 2]), 2 ** 3 ** 2)  # → 64 512
import operator
try:
    operator.pow(2, 10, 7)
except TypeError as e:
    print(e)  # → pow expected 2 arguments, got 3

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy