BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Add two Matrices

By Chaitanya Singh | Filed Under: Python Examples

In this article, we will see how to add two matrices in Python. Before we see how to implement matrix addition in Python, lets see what it looks like:

M1 = [[1,1,1],
      [1,1,1],
      [1,1,1]]
 
M2 = [[1,2,3],
      [4,5,6],
      [7,8,9]]
 
Sum of these matrices:
   = [[2,3,4],
      [5,6,7],
      [8,9,10]]

Program for adding two matrices

To represent a matrix, we are using the concept of nested lists. All the elements of both the input matrices are represented as nested lists. All the elements of output list are initialized as zero.

We are iterating the matrix and adding the corresponding elements of both the given matrices and assigning the value in the output matrix.

# This program is to add two given matrices
# We are using the concept of nested lists to represent matrix

# first matrix
M1 = [[1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]]

# second matrix
M2 = [[1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]]

# In this matrix we will store the sum of above matrices
# we have initialized all the elements of this matrix as zero
sum = [[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]]

# iterating the matrix
# rows: number of nested lists in the main list
# columns: number of elements in the nested lists
for i in range(len(M1)):
    for j in range(len(M1[0])):
        sum[i][j] = M1[i][j] + M2[i][j]

# displaying the output matrix
for num in sum:
    print(num)

Output:

[2, 3, 4]
[5, 6, 7]
[8, 9, 10]

Related Python Examples

  1. Python Program to add two binary numbers
  2. Python Program to find factorial of number
  3. Python Program to check leap year
  4. Python Program to add two numbers
  5. Python program to print Hello World

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap