Python Program to Add Two Matrices


In this example, we will learn how to add two three-order matrices in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a=[[1,2,3],[3,2,1],[4,5,6]]
b=[[1,2,3],[3,2,1],[4,5,6]]
c=[[0,0,0],[0,0,0],[0,0,0]]

for i in range(0,len(a)):
    for j in range(0,len(a[i])):
        c[i][j] = a[i][j] + b[i][j]

print('The output of adding these two matrices is:')
print(c)

Output:

The output of adding these two matrices is:
[[2, 4, 6], [6, 4, 2], [8, 10, 12]]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement