+ 1

How to define a product function (capital pi)

3rd Apr 2018, 4:28 PM
Sanjana Anna Lukose
Sanjana Anna Lukose - avatar
3 Réponses
+ 3
def capital_pi(L): # L expected to be a list p = 1 for n in L: p = p * n return p
3rd Apr 2018, 10:49 PM
Pedro Demingos
Pedro Demingos - avatar
+ 2
Like this? def Product (a, b): return a*b print (Product (5, 10))
3rd Apr 2018, 4:52 PM
Paul
Paul - avatar
+ 2
def prod(f, initial, final, step=1): _prod = 1 for x in range(initial, final, step): _prod *= f(x) return _prod The limitation of this(and any computer) is that the elements to be multiplied must be finite. You could change this definition to take any set as input, instead of the range function.
3rd Apr 2018, 5:07 PM
Bebida Roja
Bebida Roja - avatar