Multiplying in Python – A Simple Guide

Multiply Python

The foundations of data analysis lie in robust arithmetic. All the complicated algorithms that one can develop can be narrowed down into basic arithmetic such as addition, subtraction, multiplication or division; even calculus is a simple means of carrying out these basic operations. That being said, we shall set out to explore carrying out one such basic operation in Python – Multiplication!

The following are the different techniques that can be deployed in Python to serve your specific multiplication need,

  • Multiplying Numbers Within a List
    • Using math.prod( )
    • Using mul( )
  • Multiplying Two Lists
    • Using for statement
    • Using numpy

Multiplying Numbers in a List Using math.prod( )

One shall get started by importing the math library in Python using,

import math

Then one shall feed in the list that one desires to be multiplied as shown below.

Creating A List Of Numbers
Creating A List Of Numbers

Let us now assign a variable R1 for the result and tie it up with the math.prod( ) command.

R1 = math.prod(L1)

Now all the numbers given within the list will be multiplied into a single numerical entity which can be viewed by using the print( ) command.

Result Of The List Multiplied
Result Of The List Multiplied

Multiplying Numbers in a List Using mul( )

Similar to the math library used above one can also summon the operator library to do the multiplication in Python. But instead of importing the entire library as we did in the previous case, we shall exclusively extract the mul function from the operator library using the following code.

from operator import mul

Once done, we shall now set a variable ‘m’ and initialize it to 1.

Importing Operator Initialising A Variable
Importing Operator Initialising A Variable

Now we shall move ahead with constructing a ‘for’ statement which multiplies each number within the list with the help of the mul() function using the following code.

for i in L1:
    m = mul(i,m)
    print(m)

What this does is print the result of the multiplication each time, two numbers are multiplied from the list. They say ‘Showing is better than telling‘, so here we go!

Results Of Multiplication
Results Of Multiplication

Here the last number displayed is the final result after multiplying all the numbers within the list. But what if one does not want to see the answer at each step and jumps right into the final result?

It can also be done by tweaking our code a bit as follows,

m = 1
for i in L1:
    m = mul(i,m)
print(m)

Running the above code gets us directly to the final result!

Only Final Result Displayed
Only Final Result Displayed

Multiplying Two Lists using the for statement

For multiplying the elements within two lists, we shall adopt a combination of range( ) & len( ) functions nested within the ‘for’ statement as given below.

R_L = [L1[i]*L2[i] for i in range(len(L1))]

What the len( ) does is return the count of numbers within the list L1 which serves as a reference for the range( ) function which in turn tells the ‘for’ statement how many times, it needs to multiply one number with the other between the 2 lists. Running the code results in,

Multiplying Two Lists Using For Statement
Multiplying Two Lists Using For Statement

Multiplying Two Lists using numpy:

This is a fairly simple technique that starts with importing the numpy library by typing,

import numpy as np

Once done, we can use the function np.multiply( ) to get the results of multiplying two lists within the blink of an eye!

R1 = np.multiply(L1, L2)
Multiplying Two Lists Using Numpy
Multiplying Two Lists Using Numpy

Summary

Now that we have reached the end of this article, hope it has elaborated on how to multiply entities using Python programming. Here’s another article that details the comparison of date and time in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are in looking to level up in Python. Cheers!