### Adding three matrices in Python Program Program !

In this article we will discuss how to add elements of the matrix in Python. it’s very easy to deal with matrices due to its simple syntax and we create matrices using nested lists in python Programming.

We simply create matrices by creating lists inside lists as follows

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

As you can see we created lists inside a list and it seems to be the same as we saw in normal mathematics.

So, now let’s take three matrices and we will add them using a simple python program

X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Y = [[9, 8, 7], [6, 5, 4], [3, 2, 1]] Z = [[5, 5, 5], [5, 5, 5], [5, 5, 5]]

To store the result we will use another matrix whose elements will be initialized with 0

result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

## Logic

Now let’s take a look at the logic to add the matrices which is common to add any number of matrices

# iterate through rows for i in range(len(X)): # iterate through columns for j in range(len(X[0])): result[i][j] = X[i][j] + Y[i][j] + Z[i][j] To print the result for r in result: print(r)

So, the logic is to add the elements of different matrices that have the same position in different matrices, for example, the elements in the first row and first column of X, Y, and Z are 1, 9, and 5, so these elements will be added first and stored in the same place inside the result matrix.

As a result, the elements we wish to add should have the same row and column in distinct matrices, and the result will be placed in the location with the same row and column.

## Code:

# Program to add three matrices using nested loop # defining matrices using list X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Y = [[9, 8, 7], [6, 5, 4], [3, 2, 1]] Z = [[5, 5, 5], [5, 5, 5], [5, 5, 5]] result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # iterate through rows for i in range(len(X)): # iterate through columns for j in range(len(X[0])): result[i][j] = X[i][j] + Y[i][j] + Z[i][j] # printing result for r in result: print(r)

**APPROACH:**

In Python, you must subtract two matrices and print their elements. To subtract the two matrices, Approach 2 employs stacked loops. Using nested loops, it outputs the components of both matrices and then subtracts the corresponding elements of the matrices to get the final matrix.

**Time complexity:**

1. The temporal complexity of printing the matrices using nested loops is O(n2), where n is the matrices’ size.

2. The time complexity of removing two matrices with stacked loops is also O(n2).

3. As a result, the overall time complexity of this method is O(n2).

**Space complexity**:

1.This technique has an O(n2) space complexity since it requires three matrices of size n2. The input matrices matrix1 and matrix2 each require n2 space, while the result matrix requires n2 space.

**Also Read: How to Build a Simple Auto Login Bot with Python**

**Also Read: Basics of Python Programming**

**Also Read: **Exception Handling In Python

**Also Read: **Python Lambda Function

**Also Read: **Python Operators

**Thanks For Reading…..**