Python program for Matrix Addition

Python program to add two matrix. It’s a basic program which help student to understand the working of nested loop in much better way.

# Program to add two matrices using nested loop
# Author : ITVoyagers Website : itvoyagers.in
itv = [[1,17,13],
    [40 ,15,60],
    [70 ,18,59]]

itv1 = [[50,28,15],
    [65,74,31],
    [44,55,96]]
# Author : ITVoyagers Website : itvoyagers.in
solution = [[0,0,0],
         [0,0,0],
         [0,0,0]]

# iterate through rows
for i in range(len(itv)):
   # iterate through columns
   for j in range(len(itv[0])):
       solution[i][j] = itv[i][j] + itv1[i][j]
# Author : ITVoyagers Website : itvoyagers.in
for k in solution:
   print(k)

Output :

[51, 45, 28]
[105, 89, 91]
[114, 73, 155]

Note : Please note that above program is compatible with Python 3 or higher version.

Please check out other practical related to Data structures

Leave a Comment