Python Program to Multiply all Numbers in List

This article is created to cover some programs in Python that multiplies all numbers in a list, in different-different ways. Here are the list of programs:

Multiply All Numbers in a List Together

The question is, write a Python program that receives 5 numbers form user and multiplies all those 5 numbers together. The program given below is answer to this question:

nums = []
print("Enter 5 Numbers: ")
for i in range(5):
  nums.append(int(input()))
mul = 1
for i in range(5):
  mul = mul*nums[i]
print("\nMultiplication Result: ")
print(mul)

Here is the initial output of this program's sample run:

python multiply all numbers in list

Now supply inputs say 1, 2, 3, 4, 5 as five numbers of list, to multiply all these elements and print the multiplication result as shown in the snapshot given below:

multiply all numbers in list python

Multiply all Numbers in a List of n Numbers

This is the modified version of previous program. That is, this program allows user to define the size of list, then further ask to enter all the numbers of given size. For example, if user enters 10 as size, then program ask to enter 10 numbers, then multiplies all 10 numbers together and prints the multiplication result like shown in the program given below:

nums = list()

print(end="Enter the Size: ")
numsSize = int(input())
print(end="Enter " +str(numsSize)+ " Elements: ")
for i in range(numsSize):
  nums.append(int(input()))

mul = 1
for i in range(numsSize):
  mul = mul*nums[i]
print("\nMultiplication Result = " +str(mul))

Here is its sample run with user input 5 as size, and 2, 4, 6, 7, 3 as five numbers:

multiply all numbers in list python

Multiply all Numbers in List except 0

What if user enters some numbers in a list, that contains one or more zero (0) ?
Then multiplying all numbers of the list gives the result as 0. Therefore this program multiplies all numbers except zero. Let's have a look at the program and its sample run given below:

nums = list()
mul = 1

print(end="Enter the Size: ")
numsSize = int(input())
print(end="Enter " +str(numsSize)+ " Elements: ")
for i in range(numsSize):
  nums.append(int(input()))
  if nums[i]!=0:
    mul = mul*nums[i]

print("\nMultiplication Result = " +str(mul))

Here is its sample run with user input, 6 as size, and 1, 3, 0, 2, 5, 4 as six numbers:

multiply all numbers in list except zero python

Multiply all Numbers in List by Given Number (Constant)

This is the last program of this article. This program is little different from all the above programs. Since this program doesn't multiply all numbers of a list together. Rather this program multiplies all numbers of list with a number entered by user. And then prints the new list after multiplying each and every element of the list with given number like shown in the program and its sample output given below:

print(end="Enter the Size: ")
numsSize = int(input())

nums = list()
print(end="Enter " +str(numsSize)+ " Elements: ")
for i in range(numsSize):
  nums.append(int(input()))

print(end="\nEnter a Number to Multiply: ")
val = int(input())

newnums = list()
for i in range(numsSize):
  newnums.append(val*nums[i])

print("\nThe New List is:")
for i in range(numsSize):
  print(end=str(newnums[i])+ " ")
print()

Here is its sample run with user input, 4 as size, 10, 20, 30, 40 as four numbers, then 3 as number to multiply with:

multiply all numbers in list by constant python

Here is another sample run with user input, 5 as size, 0, 1, 2, 3, 4 as five elements, and 2 as number to multiply with:

python multiply all elements in list

Python Online Test


« Previous Program Next Program »