Q:

Python program to multiply all numbers of a list

belongs to collection: Python List Programs

0

Multiply all numbers of a list

We will take a list as input from the user. And return the product of all numbers of the list.

Example:

Input:
[4, 1, 6, 3, 9]

Output:
648

We simply need to find the product of all numbers. This task can be performed in multiple ways in python.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1: Using loops

To find the product of all elements of a list, we will simply traverse the list and then multiply all values to a product variable. At the end return the product.

Algorithm:

  • Take input from the user.
  • Loop over the array.
    • productVal *= i.
  • Return the productVal.

Program to multiply all numbers of a list

# Python program to multiply all numbers of a list

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# multiplying all numbers of a list
productVal = 1
for i in myList:
    productVal *= i

# Printing values 
print("List : ", myList)
print("Product of all values = ", productVal)

Output:

Enter number of elements: 5
4
1
6
3
9
List :  [4, 1, 6, 3, 9]
Product of all values =  648

Method 2: Using prod method from math library

Python also provides its users with a special method to perform tasks.

One such method is prod() which accepts a list (or iterable) and returns the product of all its elements.

Syntax:

math.prod(list_name)

Return a value based on the type of list which is the product of all numbers of the list.

Program to multiply all numbers of a list

# Python program to multiply all numbers of a list

import math

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# multiplying all numbers of a list
productVal = math.prod(myList)

# Printing values 
print("List : ", myList)
print("Product of all values= ", productVal)

Output:

Enter number of elements: 3
3
5
7
List :  [3, 5, 7]
Product of all values= 105

Method 3: Using numpy's prod() method

You can alternatively use the method prod() from numpy library. As Python has many libraries that provide its users multiple functions to perform tasks quickly, you have multiple options to do it.

The numpy.prod() method takes in the list and returns the product of all values of the list.

Syntax:

numpy.prod(list_name)

Program to multiply all numbers of a list

# Python program to multiply all numbers of a list

import numpy

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# multiplying all numbers of a list
productVal = numpy.prod(myList)

# Printing values 
print("List : ", myList)
print("Product of all values= ", productVal)

Output:

Enter number of elements: 5
4
1
6
3
9
List :  [4, 1, 6, 3, 9]
Product of all values=  648

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to find the length of a list in Python (3 effe... >>
<< Python program to remove empty list from a list of...