Python program to multiply all numbers of a list

Here, we are going to learn how to multiply all numbers of a list in Python? By Shivang Yadav Last updated : September 17, 2023

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

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

Consider the below example with sample input and output:

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.

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

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.