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.
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:
Program to multiply all numbers of a list
Output:
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:
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
Output:
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:
Program to multiply all numbers of a list
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer