Write a Python Program to Multiply Two Matrices

Python Program to Multiply Two Matrices

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Python Program to Multiply Two Matrices
Python Program to Multiply Two Matrices

Write a Python Program to Multiply Two Matrices

PROGRAM: Python Program to Multiply Two Matrices

/* Python Program to Multiply Two Matrices */

# Program to multiply two matrices using nested loops

# 3×3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3×4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3×4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]

for r in result:
print(r)

OUTPUT :-

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

Leave a Reply

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