Python program to find the multiplication of all elements in a list

Python program to find the multiplication of all elements in a list :

In this tutorial, we will learn how to multiplies all elements of a list and print out the result. First of all, we will add some numbers to a list and then using a for loop we will calculate the multiplication of all elements. Let’s take a look into the program first :

Python 3 program to multiply all items in a list :

#1
my_list = []

#2
for i in range(1,5):
	my_list.append(i)

#3
print(my_list)

#4
result = 1

#5
for item in my_list:
	result = result * item

#6
print("multiplication of all elements : ",result)

Explanation :

The commented numbers in the above program denotes the step number below :

1. Create one empty list my_list
2. Add elements to this list using a for loop. The loop will add elements starting from 1 to 4.
3. Print the list to the user.
4. Declare one variable result as 1. This variable will hold the final multiplication result.
5. Run one for loop. This loop will multiply all items of the list and save it in result variable.
6. Finally, print out the result .

Sample Output :

[1, 2, 3, 4]
('multiplication of all elements : ', 24)

Currently we are inserting only four elements to the list and calculating the multiplication. You can add as many items as you want.

Leave a Reply