+ 1
How to define a product function (capital pi)
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
+ 2
Like this?
def Product (a, b):
return a*b
print (Product (5, 10))
+ 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.