Matrix multiplication in Python using user input

In this post, we will see a how to take matrix input from the user and perform matrix multiplication in Python.

Let’s see the example first.
Example:

Now, let’s see the different ways to do this task:

Using Nested loops(for / while).

In this method, we have to iterate through each row and each column items that’s why we use nested loops here.
Below is the Python code given:

Output:

Using dot() method of numpy library.

In this method, dot() method of numpy is used. dot() method is used to find out the dot product of two matrices. dot product is nothing but a simple matrix multiplication in Python using numpy library. We have to pass two matrices in this method for which we have required dot product.

Below is the Python code given:

Output:

Using list-comprehension and zip() function.

In this method, zip() function is used to combine list and generated zip object which is the object of list of tuples and list-comprehension is used for creating list and perform operation in one-line code.

Below is the Python code given:

Output:

That’s all about Matrix multiplication in Python using user input.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *