Python Examples

Python Program to Add Two Matrices

There are numerous methods for matrix addition python. This can be done using the for loop, using nested list comprehension, and also by using the zip().

For example:

Input :


X= [[8,6,3],

    [4 ,5, 6],

    [7 ,8, 3]]


Y = [[2,5,5],



Output :



result= [[10,11,8],



matrix addition python

Source Code

Source code: Matrix Addition using Nested Loop

In the matrix addition python program using the nested loops, the program is used to iterate through every row and every column. The compiler adds the corresponding elements in the two matrices at each position and stores the result. A matrix can be implemented as a hierarchical list in Python (list inside a list). Each element in the matrix can be thought of as a row.

Source Code


A = [[12,7,3],

    [4 ,5,6],

    [7 ,8,9]]

B = [[5,8,1],



result = [[0,0,0],



# iterate through rows

for x in range(len(A)):

   # iterate through columns

   for y in range(len(A[0])):

       result[x][y] = A[x][y] + B[x][y]

for q in result:



[17, 15, 4]

[10, 12, 9]

[11, 13, 18]

Source Code: Matrix Addition using Nested List Comprehension


matrix1 = [[1,1,1], [2,2,2], [3,3,3]]

matrix2 = [[4,6,4], [5,6,5], [6,5,6]]

# The matrices are added using the for loop.

# Assume that the matrices are of the same size.

matrixsum = []

for row in range(len(matrix1)):


for column in range(len(matrix[0])):

matrixsum[row].append(matrix1[row][column] + matrix2[row][column])

#Only use list comprehension for inner lists when rewriting.

matrixsum = []

for row in range(len(matrix1)):

matrixsum.append([matrix1[row][column] + matrix2[row][column] for column in range(len(matrix[0]))])

# When rewriting use the nested list comprehension.

matrixsum = [[matrix1[row][column] + matrix2[row][column] for column in range(len(matrix[0]))]

      for row in range(len(matrix1))]



[[5, 7, 5], [7, 8, 7], [9, 8, 9]]

How do you add to a matrix in python?

Suppose we have two matrices E and D.

Example 1:

E = [[1,2],[3,4]]

DY = [[4,5],[6,7]]

then we get

E+D = [[5,7],[9,11]]

Example 2:

E = [[1,2],[3,4],[8,3]]

D = [[4,5],[6,7],[4,7]]

then we get

E+D = [[5,7],[9,11],[12,10]]

Look at the example given below to understand how to add the elements in Python:

Source Code:


import numpy as np

  # The first matrix will be

P = np.array([[1, 2], [3, 4]])

#The second matrix will be

Q = np.array([[4, 5], [6, 7]])

print("Elements of the first matrix")


print("Elements of the second matrix")


  # adding two matrix

print("The sum of the two matrices is")

print(np.add(P, Q))


Elements of the first matrix

[[1 2]

 [3 4]]

Elements of the second matrix

[[4 5]

 [6 7]]

The sum of the two matrices is

[[ 5 7]

 [ 9 11]]

How do you find the sum of a matrix in python?

The sum of the matrices is obtained using the for loop, using nested list comprehension, and by using the zip().

Source Code for finding the sum of the matrices using the zip():

The zip() function takes an iterator for each matrix element, maps them, and then adds them using the sum() function. It returns the result and saves it in the mapping form.


X = [[1,2,3],

    [4 ,0,6],

    [7 ,8,9]]

Y = [[9,4, 7],



result1= [map(sum, zip(*t)) for t in zip(X, Y)]


Can you add a 2×2 and a 2×3 matrix?

No. It is not possible to add a 2×2 and a 2×3 matrix. For the addition of matrices, it is important that the matrices have the same dimensions. Adding a 2×2 and 2×3 matrix is not feasible, but you can add a 2×3 matrice with a 2×3 or a 3×3 with a 3×3

How do you add two matrices in Python using Numpy?

Using the matrix.sum() we can add the sum of two matrices. 

Syntax : 



Return the sum of a matrix’s values.

Example 1:


import numpy as np
#Numpy can be used to create matrices.

abc = np.matrix('[4, 1; 12, 3]')   

# applying matrix.sum() method

g = abc.sum()




Example 2:


import numpy as np    

# make matrix with numpy

abc = np.matrix('[4, 1; 12, 3]')      

# applying matrix.sum() method to obtain the sum of the matrices

gg = abc.sum()



Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.