Multiplying list terms

2

I want to print the multiplication of terms and I would like to know if there is any function that can do this for me in the Python language:

lista = []

con = 0

while(con<5):
    n = int(input('insira um número inteiro'))

   con = con + 1

   lista.append(n)

   print('lista criada=',lis
ta)

   print('soma das listas=',sum(lista))
    
asked by anonymous 16.10.2018 / 11:56

2 answers

1

You can also use numpy.prod() to get the multiplication of all elements in the list.

Example:

import numpy

lista = [1, 2, 3, 4, 5]
resultado = numpy.prod(lista)
print(resultado)

Source: link

    
23.10.2018 / 00:09
3

There is no proper function for this, but there are ways you can not have to do everything at hand. One of the simplest ways is to combine the use of % with% functions. with functools.reduce :

from functools import reduce
from operator import mul

lista = [1, 2, 3, 4, 5]
produto = reduce(mul, lista, 1)
print(produto)  # 120

Or, iteratively:

produto = 1
for numero in lista:
    produto *= numero
print(produto)  # 120
    
16.10.2018 / 13:17