
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program addition of two matrix
Given two user input matrix. Our task is to display the addition of two matrix. In these problem we use nested List comprehensive.
Algorithm
Step1: input two matrix. Step 2: nested for loops only to iterate through each row and columns. Step 3: At each iterationshall add the corresponding elements from two matrices and shall store the result.
Example code
# Program to add two matrices using nested loop A=[] n=int(input("Enter N for N x N matrix : ")) #3 here #use list for storing 2D array #get the user input and store it in list (here IN : 1 to 9) print("Enter the element ::>") for i in range(n): row=[] #temporary list to store the row for j in range(n): row.append(int(input())) #add the input to row list A.append(row) #add the row to the list print(A) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] #Display the 2D array print("Display Array In Matrix Form") for i in range(n): for j in range(n): print(A[i][j], end=" ") #new line print() B=[] n=int(input("Enter N for N x N matrix : ")) #3 here #use list for storing 2D array #get the user input and store it in list (here IN : 1 to 9) print("Enter the element ::>") for i in range(n): row=[] #temporary list to store the row for j in range(n): row.append(int(input())) #add the input to row list B.append(row) #add the row to the list print(B) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] #Display the 2D array print("Display Array In Matrix Form") for i in range(n): for j in range(n): print(B[i][j], end=" ") print() #new line result = [[0,0,0], [0,0,0], [0,0,0]] # iterate through rows for i in range(n): # iterate through columns for j in range(len(A[0])): result[i][j] = A[i][j] + B[i][j] print("Resultant Matrix is ::>") for r in result: print("Resultant Matrix is ::>",r)
Output
Enter N for N x N matrix : 3 Enter the element ::> 10 10 10 20 20 20 30 30 30 [[10, 10, 10], [20, 20, 20], [30, 30, 30]] Display Array In Matrix Form 10 10 10 20 20 20 30 30 30 Enter N for N x N matrix : 3 Enter the element ::> 100 100 100 200 200 200 300 300 300 [[100, 100, 100], [200, 200, 200], [300, 300, 300]] Display Array In Matrix Form 100 100 100 200 200 200 300 300 300 Resultant Matrix is ::> [110, 110, 110] [220, 220, 220] [330, 330, 330]
- Related Articles
- Python program multiplication of two matrix.
- How to combine two rows in R matrix by addition?
- How to perform Matrix Addition using C#?
- Addition of two number using ‘-‘ operator?
- C++ program to overload addition operator to add two matrices
- Addition and Subtraction of Matrix using pthreads in C/C++
- Addition of tuples in Python
- Addition of two numbers without propagating Carry?
- Explain the addition of two negative integers.
- C++ program to overload addition operator to add two complex numbers
- Bitwise recursive addition of two integers in C
- Python Program to check Involutory Matrix
- Python Program to find the transpose of a matrix
- Find the transpose of a matrix in Python Program
- Python Program to Print an Identity Matrix

Advertisements