Python Glossary Русский

Python 3.12+Numbers & mathfunction

math.sumprod

product

The sum of products of two sequences (dot product; 3.12+).

Syntax

math.sumprod(...)

Unlike zip, mismatched lengths are a ValueError here, not a silent truncation. For floats it is usually more accurate than sum(a * b for a, b in zip(p, q)) because the accumulation carries extra precision, and for ints the arithmetic stays exact and integral.

Examples

import math
print(math.sumprod((1, 2, 3), (4, 5, 6)))   # → 32
print(math.sumprod([100, 250, 30], [2, 1, 5]))   # → 600
print(math.sumprod([], []))   # → 0
print(math.sumprod([1, 2, 3], [4, 5, 6]) == sum(x * y for x, y in zip([1, 2, 3], [4, 5, 6])))   # → True
print(math.sumprod([1, 2, 3], [4, 5]))   # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy