Historically, Guido vetoed the idea: http://bugs.python.org/issue1093
As noted in that issue, you can make your own:
Copyfrom functools import reduce # Valid in Python 2.6+, required in Python 3
import operator
reduce(operator.mul, (3, 4, 5), 1)
Answer from ojrac on Stack OverflowHistorically, Guido vetoed the idea: http://bugs.python.org/issue1093
As noted in that issue, you can make your own:
Copyfrom functools import reduce # Valid in Python 2.6+, required in Python 3
import operator
reduce(operator.mul, (3, 4, 5), 1)
In Python 3.8, the prod function was added to the math module. See: math.prod().
Older info: Python 3.7 and prior
The function you're looking for would be called prod() or product() but Python doesn't have that function. So, you need to write your own (which is easy).
Pronouncement on prod()
Yes, that's right. Guido rejected the idea for a built-in prod() function because he thought it was rarely needed.
Alternative with reduce()
As you suggested, it is not hard to make your own using reduce() and operator.mul():
Copyfrom functools import reduce # Required in Python 3
import operator
def prod(iterable):
return reduce(operator.mul, iterable, 1)
>>> prod(range(1, 5))
24
Note, in Python 3, the reduce() function was moved to the functools module.
Specific case: Factorials
As a side note, the primary motivating use case for prod() is to compute factorials. We already have support for that in the math module:
Copy>>> import math
>>> math.factorial(10)
3628800
Alternative with logarithms
If your data consists of floats, you can compute a product using sum() with exponents and logarithms:
Copy>>> from math import log, exp
>>> data = [1.2, 1.5, 2.5, 0.9, 14.2, 3.8]
>>> exp(sum(map(log, data)))
218.53799999999993
>>> 1.2 * 1.5 * 2.5 * 0.9 * 14.2 * 3.8
218.53799999999998
Note, the use of log() requires that all the inputs are positive.
Writing Code for Summation Formula with Given Variables
Python: multiply all elements in a list besides the one you are iterated on
Multiplying in Python Without Using * - Help Please
Multiply two columns based on a condition in a Pandas Dataframe?
Videos
I'm only in my first week of my python class. How do I write code to calculate the sum of a sequence (s) with initial value (a), amount added at each step (d) and number of terms (n). So if a=1, d=2 and n=5, calculate and report the sum of 1+3+5+7+9. For the summation formula: S=N/2(2A + (N-1) * D)
A = 1
print("Enter the Value of n: ")
n = int(input())
A = 1
sum = 0
#This is how far I've gotten on PyCharm with working code and I cannot figure it out for the life of me