Articles

# Program to multiply two matrices in Python

In the python programming article, we are going to learn

• program to find the product of two matrices in python or python program to multiply two matrices using nested loops

### Program to multiply two matrices in python

The program to multiply two matrices in python is as follows:

``````# Owner : TutorialsInhand Author : Devjeet Roy

mat1 = [[1,2,3],
[4,5,6],
[7,8,9]]

mat2 = [[11,12,13],
[14,15,16],
[17,18,19]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

for i in range(len(mat1)):
for j in range(len(mat2)):
for k in range(len(mat2)):
result[i][j] += mat1[i][k] * mat2[k][j]

for r in result:
print(r)``````

The output of python program to multiply two matrices is as follows:

``````PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
[90, 96, 102]
[216, 231, 246]
[342, 366, 390]``````

### Few important tips about the program

1. In the program we have considerd row of the first matrix with the column of the second, multiplied each elements and have accumulated the sum. The same process has been repeated for all.

2.The time complexity of the algorithm is O(n3)

Python program to multiply two matrices snapshot is given below: In above program to multiply two matrices in python we have taken two matrix mat1 and mat2 and a result matrix that stores the result of operation performed on matrix in every loop.

Thus we have completed our python program to multiply two matrices using nested loops.

Python Numbers Program
Python Miscellaneous Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com 