In this python tutorial, you will learn how to Add Two Matrices Using Multi-dimensional Arrays with for loop of the python programming language.
How to Add Two Matrices Using Multi-dimensional Arrays?
Let’s take a look at the source code, here the values are assigned in the code, the for loop carry out the function.
RUN CODE SNIPPET#Python Program to Add Two Matrices Using Multi-dimensional Arrays
X = [[1,2,3],
    [4 ,5,6],
    [7 ,8,9]]
Y = [[9,8,7],
    [6,5,4],
    [3,2,1]]
result = [[X[i][j] + Y[i][j] for j in range
(len(X[0]))] for i in range(len(X))]
for r in result:
    print(r)
OUTPUT:
[10, 10, 10] [10, 10, 10] [10, 10, 10]
- At first, we enter the values of two matrices with three rows and three columns. We assign those values to the variables XandY. The values are enclosed within square brackets and closed on the whole with the same.
- Then we declare the variable resultwith the condition[X[i][j] + Y[i][j]Â after which we declare anforloop where therangefor the variableiis(len(X[0]))
- We declare another forloop where therangefor the variablejis(len(X)), this whole statement in enclosed in a square braces.
- We declare a final forloop where theresultis the variablej, following that we give aprintfunction which will display the values stored in the variabler
NOTE:
- The print function is used to display the value, it prints the specified message to the screen
- Â The For loops are used to loop through an iterable object and perform the same action for each entry.
- The len() is a built function  used to calculate the length of any iterable object
 
															 
								