Write a Python function to multiply all the numbers in a list




   
 example 1:-
def multiply(numbers):  
    total = 1
    for n in numbers:
        total *= n  
    return total  
print(multiply((12,4,5,6,2)))

output:-
2880
>>> 

example 2:-

def multiply(numbers):  
    total = 1
    for x in numbers:
        total *= x  
    return total  
print(multiply((-2,9,-3,5,6)))
output:-
1620
>>> 

example 3:-

# Python program to multiply all values in the
# list using traversal
 
def multiplyList(myList) :
     
    # Multiply elements one by one
    result = 1
    for x in myList:
         result = result * x 
    return result 
     
# Driver code
list1 = [1, 2, 3] 
list2 = [3, 2, 4]
print(multiplyList(list1))
print(multiplyList(list2))
output:-
6
24
>>> 

example 4:-

# Python3 program to multiply all values in the
# list using numpy.prod()
 
import numpy 
list1 = [1, 2, 3] 
list2 = [3, 2, 4]
 
# using numpy.prod() to get the multiplications 
result1 = numpy.prod(list1)
result2 = numpy.prod(list2)
print(result1)
print(result2)
output:-
6
24
>>> 

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post