Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
math
product
communitycreator

What is math.prod() in Python?

Hassan Ahmed

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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))

RELATED TAGS

python
math
product
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring