Saturday, January 28, 2017

Addition and Product of a List Python

Tags

Question::
Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.


Code::

def add(l):
    s=0
    for j in l:
        s=s+int(j)
    return s

def multiply(l):
    m=1
    for j in l:
        m=m*int(j)
    return m

d=input("Enter List: ")
n=d.split(",")
print("Entered List=",n)
print("Addition is:",add(n))
print("Product is:",multiply(n))


Output::