Python Glossary Русский

all Python 3operatorfunction

operator.mul

arithmetic

Return a * b (the functional form of `*`).

Syntax

operator.mul(a, b)

The classic use is the product of items: functools.reduce(operator.mul, xs, 1). The initial 1 is required, otherwise an empty list raises TypeError. Since Python 3.8 math.prod does the same and reads better. For sequences mul means repetition: mul([0], 3) gives [0, 0, 0], and nested mutable items are not copied.

Examples

import operator
import functools
print(operator.mul(3, 4))   # → 12
print(operator.mul(3, 4) == 3 * 4)   # → True
print(functools.reduce(operator.mul, range(1, 6)))   # → 120
print(operator.mul('ab', 3))   # → ababab
print(list(map(operator.mul, [1, 2, 3], [10, 10, 10])))   # → [10, 20, 30]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy