Home Python math Functions prod() Python math.prod() Function Definition math.prod(iterable, *, start=1) multiplies all items of iterable together, starting from start (which defaults to 1).
Added in Python 3.8 as the multiplicative counterpart to built-in sum() , it preserves exact int results when all inputs are integers and returns start when the iterable is empty.
math.prod() is a function in Python's standard math module: import it with import math and call math.prod(...), or import the name directly with from math import prod. Added in Python 3.8.
Syntax Import math and pass an iterable, plus optional keyword-only start (Python 3.8+).
import math
total = math.prod(values)
scaled = math.prod(values, start=10) Call What it does math.prod([2, 3, 4])Returns 24 (int). math.prod([2, 3, 4], start=10)Returns 240 (keyword-only start). math.prod([])Returns 1 (default start for empty iterable). math.prod([2, 0.5])Returns 1.0 (float promotion).
Notice start is keyword-only : passing math.prod([2, 3], 10) positionally raises TypeError, unlike built-in sum().
Function signatures math.prod(iterable, /, *, start=1)
# added in Python 3.8; start is keyword-only Parameters Parameter Description iterableRequired positional iterable of numbers (list, tuple, range, generator, etc.). startOptional keyword-only starting value, defaulting to 1.
Return Value Return Value Description int or floatExact int when all items and start are ints; promotes to float if any element is a float.
Python Examples 1. Compound factors 2. Keyword-only start 3. Generators and promotion Example 1 — Multiply Growth Factors and Integer Lists Chain yearly investment multipliers and integer factors cleanly without writing a loop.
import math
factors = [1.05, 1.02, 0.98, 1.10]
growth = math.prod(factors)
print(f"net factor: {growth:.4f}")
print("int product:", math.prod([2, 3, 4, 5])) net factor: 1.1545
int product: 120 How It Works Chained float multipliers. Multiplying all four yearly ratios calculates total cumulative growth in one call.Formatted net multiplier. Four decimal places show a net 15.45% gain across the period.Integers stay integers. Multiplying [2, 3, 4, 5] returns exact integer 120.Example 2 — Use Keyword start and Handle Empty Iterables Scale products with start=10, inspect empty lists, and catch positional-arg TypeError.
import math
print(math.prod([2, 3, 4], start=10))
print(math.prod([]), math.prod([], start=5))
try:
math.prod([2, 3], 10)
except TypeError:
print("start must be passed by keyword") 240
1 5
start must be passed by keyword How It Works Initial multiplier. Setting start=10 begins accumulation at ten instead of one, yielding 240.Empty iterable returns start. An empty list returns default 1, or custom start=5.Keyword-only enforcement. Passing 10 positionally raises TypeError.Example 3 — Multiply Ranges, Generators, and Mixed Numeric Types Compute factorials from range(), multiply primes, and watch float promotion.
import math
print("7! via range:", math.prod(range(1, 8)))
primes = (p for p in [2, 3, 5, 7, 11, 13])
print("primorial :", math.prod(primes))
mixed = math.prod([2, 3, 0.5])
print("mixed :", mixed, type(mixed).__name__) How It Works Range product. Passing range(1, 8) multiplies 1 through 7 cleanly without creating a list.Lazy generator input. Any generator expression works directly inside math.prod().Float promotion rule. Including a single float 0.5 promotes the product to 3.0 (float).Frequently Asked Questions Which Python version added math.prod()? math.prod() was added in Python 3.8.
Why does math.prod([2, 3], 10) raise TypeError? Because start is a keyword-only parameter in math.prod(). Write math.prod([2, 3], start=10).
What does math.prod([]) return? 1, the default value of start.
Does math.prod() return an int or a float? It returns an int if all inputs and start are integers, and promotes to float (or complex/Decimal) according to normal Python multiplication rules.
How is math.prod() related to sum()? prod() is the multiplicative equivalent of built-in sum(), starting at 1 instead of 0.
Next: math.radians() Convert angle x from degrees to radians.
radians() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion