What is math.prod() in Python?

The built-in math.prod() function from the math module in Python is used to return the products of elements in an iterable object like a list or a tuple.

Syntax

math.prod(iterable, start)

Parameters and return value

The math.prod() function requires an iterable as its first parameter, which can be a list or a tuple.

An optional start parameter can also be passed that represents the starting value of the product. The default value of this parameter is 1.

This function calculates the products of all the elements in the specified iterable that was passed as an argument.

When the passed iterable is empty, the start value is returned.

Code

List and tuple passed as an argument

import math
#List passed as iterable
list_1 = [2, 2, 2, 2]
print(math.prod(list_1))
#Tuple passed as iterable
tuple_1 = (3, 3, 3, 3)
print(math.prod(tuple_1))

Start value passed as an argument

import math
#Start value passed as an argument
list_1 = [2, 2, 2, 2]
print(math.prod(list_1, start=2))

Empty iterable passed as an argument

import math
#Empty iterable passed as argument
list_1 = []
print(math.prod(list_1))
#Empty iterable and optional start value passed as arguments
print(math.prod(list_1, start=3))
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources