Python Tutorial Python Advanced Python References Python Libraries

Python math - prod() Method



The Python math.prod() function returns the product of all the elements in the input iterable multiplied by start. The default value of start is 1. When the iterable is empty, the function returns the value of start parameter.

Syntax

#New in version 3.8
math.prod(iterable, start=1)

Parameters

iterable Required. Specify the iterator whose elements need to be multiplied.
start Optional. Specify the start value. Default is 1.

Return Value

Returns the product of all the elements in the input iterable.

Example:

In the example below, prod() function returns the product of all the elements in the input iterable.

import math

MyList = [1, 2, 3]
#product of all elements of the list
print(math.prod(MyList))

#product when start=2
print(math.prod(MyList, start=2))

#using method with empty list
MyList.clear()
print(math.prod(MyList, start=2))

The output of the above code will be:

6
12
2

❮ Python Math Module

5