How to Perform Multiplication in Python?

Multiplication In Python

There are different ways to perform multiplication in Python. The most simple one is using the asterisk operator(*), i.e., you pass two numbers and just printing num1 * num2 will give you the desired output.

This tutorial will guide you through the different ways to do multiplication in Python. We will also learn how to write code in Python to get the multiplication of elements of a list given as input.

Multiplication of two numbers in Python

For simplicity, let’s see Multiplication in Python with two numbers entered by the user.

1. Without using a function

Let’s see an example of printing the product of two numbers without using a function. We’ll simply print the results.

Example:

n1 = int(input("Enter a number:")
n2 = int(input("Enter another number:")

res = n1 * n2

print("The product is ", res)

Output:

Enter a number: 3
Enter another number: 5
The product is 15 

2. With a function

If it is required to perform multiplication in your program several times, then you must create a function that will return the product of the numbers passed to it while calling. This will reduce the complexity of your program and introduce reusability, i.e., you can call the same function again and again with a different set of arguments. Let us see an example to demonstrate this.

Example:

def mult(a , b):
  return a * b

n1 = int(input("Enter a number :"))
n2 = int(input("Enter another number :"))
multiplication1 = mult(n1 , n2)

num1 = 6.0
num2 = 5.0
multiplication2 = mult(num1 , num2)

print("The product of {} and {} is {}".format(n1 , n2 , multiplication1))
print("The product of {} and {} is {}".format(num1 , num2, multiplication2)

Output:

Enter a number : 4
Enter another number : 7
The product of 4 and 7 is 28
The product of 6.0 and 5.0 is 30.0

Here, we have defined a function named mult which returns the product. We are calling this function twice in our code. First, using user entered integers values. Second, using the prespecified float values. Hence, proving reusability.

Perform multiplication operations on elements of a list in Python

We can also get the product of all the elements of a given list using different ways.

1. By traversing through a list

In this method, we will use a for loop to traverse through the list and a variable ‘res’ that is initialized to 1(not 0 because we need the product and 0 * anything = 0). The value of ‘res’ is updated with each iteration.

Example:

list1 = [3,4,5]

res = 1

for val in list1:
  res = res * val

print("The product of elements of the given list is ", res)

Output:

The product of elements of the given list is  60

2. Using numpy.prod()

In the NumPy, we have the prod() function which takes a list as an argument and returns the product of all the elements in that list. This function is of great use and saves a lot of code. You just need to import NumPy to use numpy.prod(). Let us see an example to demonstrate this.

Example:

import numpy

list1 = [2,3,4,5]
list2 = [10,10,10]

ans1 = numpy.prod(list1)
ans2 = numpy.prod(list2)

print("Multiplication of elements of list1 is ",ans1)
print("Multiplication of elements of list2 is ",ans2)

Output:

Multiplication of elements of list1 is  120
Multiplication of elements of list2 is  1000

Conclusion

We hope that instead of just learning how to multiply using *, you have learned many more ways to do multiplication in Python, which will definitely help you in your Python learning journey. We also introduced you to the numpy.prod() method for multiplying all the elements of a list in one go. Hope you enjoyed reading this tutorial.

Reference

https://stackoverflow.com/questions/13840379/how-can-i-multiply-all-items-in-a-list-together-with-python