+ 1

How to calculate thé product of numbers in list on python

Hello I want print numbers from a list and calculate their product , can you help me please. For example A=['2','3','5','9',........] The result must be 2*3*5*9 Thank you

27th Dec 2019, 8:02 PM
iren yeger
iren yeger - avatar
4 Réponses
+ 4
https://code.sololearn.com/cHV9S0QMnxos/#py The playground uses Python version 3.7.5. If they have Python 3.8.x, there is a function math.prod that you can use. There are other ways to get the product of numbers such as using the reduce function, but I prefer the for-loop way. Hope that helps.
27th Dec 2019, 8:46 PM
r1c5
r1c5 - avatar
+ 2
This would be another version. You can apply that for other operations than multiplication as well. from functools import reduce print(reduce((lambda x, y: x*y), [3, 5, 7]))
28th Dec 2019, 12:58 AM
HonFu
HonFu - avatar
+ 1
r1c5 Thank you
27th Dec 2019, 9:26 PM
iren yeger
iren yeger - avatar
+ 1
Or this: eval('*'.join(A))
5th Jan 2021, 9:52 AM
abhinav
abhinav - avatar