**Python Matrix multiplication** is an operation that takes two **matrices** and multiplies them. Multiplication of two matrices is possible when the first matrix’s rows are equal to the second matrix columns. It multiplies the row items of the first matrix with the column items of the second matrix. The **syntax** for a matrix can be as an array inside an array.

Table of Contents

## Definition of Matrix

A python matrix is a specialized two-dimensional array. A rectangular array that stores data in rows and columns. Array is the one of important data structures that a user use to perform mathematical and scientific calculations. Data inside the matrix can be numbers, strings, expressions, symbols, etc.

There are three ways of doing matrix multiplication.

- Using For Loop
- with List
- and NumPy Library

## Explicit For Loops

Explicit for loops is a simple technique for the Multiplication of two matrices. But, this technique is one of the expensive methods for larger matrix operations. Let’s see an example in which we will use nested for loops for matrix multiplication. It will multiply each row from the first matrix to each column element from the second matrix.

### Example: Python Matrix Multiplication of order 3x3 matrix using nested for loops

```
matrix1 = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
matrix2 = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0 for x in range(3)] for y in range(3)]
# explicit for loops
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
# resulted matrix
result[i][j] += matrix1[i][k] * matrix2[k][j]
print("Resultant Matrix : ", result)
```

### Output Matrix

## List Comprehension

We can use the list comprehension technique as well for the Multiplication of two matrices. The whole process will be the same as in nested for loops, but we used for loop to store the result in the above example. Here in this example, we will create a list to store the result. Let’s have a look at an example of this.

### Example: Python Matrix Multiplication of order 3x3 matrix using list comprehension

```
Matrix1 = [[1, 2, 3],
[4,5,6],
[7,8,9]]
Matrix2 = [[9,8,7],
[6,5,4],
[3,2,1]]
RM = [[0,0,0],
[0,0,0],
[0,0,0]]
matrix_length = len(Matrix1)
for i in range(len(Matrix1)):
for k in range(len(Matrix2)):
RM[i][k] = Matrix1[i][k] * Matrix2[i][k]
print("Multiplication of two matrices using list comprehension: ", RM)
```

### Output Matrix

## NumPy Module

Multiplication of two matrices using **NumPy** is also known as vectorization. Using this module to reduce explicit use of for loops in the program makes program execution faster. NumPy is a built-in package of Python which is used for array processing and manipulation. We need to import NumPy in the program and use dot operator for matrix multiplication to use this package. Let’s have a look at an example.

### Example: Python Matrix Multiplication of order 3x3 matrix using list comprehension

```
import NumPy as np
matrix1 = ([1, 2, 3],[4 ,5, 6],[7, 8, 9])
matrix2 = ([9, 8, 7],[6, 5, 4],[3,2, 1])
result = np.dot(matrix1,matrix2)
print("Matrix Multiplication Using NumPy : ", result)
```

### Output Matrix

## Conclusion

In this article, we discussed the Multiplication of two matrices. We discussed three different ways of python **matrix multiplication.** And coding examples in which we performed Multiplication of **3×3 matrix**