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.
math.prod(iterable, start)
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.
import math#List passed as iterablelist_1 = [2, 2, 2, 2]print(math.prod(list_1))#Tuple passed as iterabletuple_1 = (3, 3, 3, 3)print(math.prod(tuple_1))
import math#Start value passed as an argumentlist_1 = [2, 2, 2, 2]print(math.prod(list_1, start=2))
import math#Empty iterable passed as argumentlist_1 = []print(math.prod(list_1))#Empty iterable and optional start value passed as argumentsprint(math.prod(list_1, start=3))