Contents

## Python – Matrix Addition

In Python, a Matrix can be represented using a nested list. Lists inside the list are the rows.

Following is a simple example of nested list which could be considered as a `2x3`

matrix.

`matrixA = [ [2, 8, 4], [3, 1, 5] ]`

Run The two lists inside `matrixA`

are the rows of the matrix. Number of elements inside a row represent the number of columns.

Also, in Python programming, the indexing start from `0`

. So, when we specify `matrixA[2][4]`

in the program, that is actually `[2+1][4+1] = [3][5]`

, element of third row and fifth column.

As we are using Lists to represent matrices, you should be careful with the results of arithmetic operator for two lists.

For example, in the following statement, the result of addition operator does not return a list of element by element addition of the two operands, but returns a list with the inner lists of second operand appended to the elements of first operand.

`matrixC = matrixA + matrixB // where these are nested lists`

Run To compute the addition of two matrices, which are list of lists in Python, you have to traverse through the rows and columns, and compute the addition.

In this tutorial, we will learn how to do Matrix Addition in Python using Lists.

### Example 1: Python Program to Add Two Matrices using For Loop

In this example, we shall take two matrices and compute their addition. We shall use Python For Loop to traverse through all the elements of a matrix.

**Python Program**

```
matrixA = [ [2, 1, 3], [3, 1, 5] ]
matrixB = [ [1, 4, 2], [5, 2, 0] ]
#matrix where we shall store our result
#intialize this to 0, with size as that of matrixA or matrixB
matrixC = [[0 for i in range(len(matrixA[0]))] for j in range(len(matrixA))]
#for each row in the matrix
for i in range(len(matrixB)):
#for each element in the row
for j in range(len(matrixB[0])):
matrixC[i][j] = matrixA[i][j] + matrixB[i][j]
print(' matrixA:', matrixA)
print('+ matrixB:', matrixB)
print('-------------------------------')
print(' matrixC:', matrixC)
```

Run **Output**

```
matrixA: [[2, 1, 3], [3, 1, 5]]
+ matrixB: [[1, 4, 2], [5, 2, 0]]
-------------------------------
matrixC: [[3, 5, 5], [8, 3, 5]]
```

### Example 2: Python Program to Add Two Matrices using List Comprehension

In this example, we shall take two matrices and compute their addition. We shall use Python List Comprehension to compute addition at element level.

**Python Program**

```
matrixA = [ [2, 1, 3], [3, 1, 5] ]
matrixB = [ [1, 4, 2], [5, 2, 0] ]
matrixC = [[matrixA[i][j] + matrixB[i][j] for j in range(len(matrixA[0]))] for i in range(len(matrixA))]
print(' matrixA:', matrixA)
print('+ matrixB:', matrixB)
print('-------------------------------')
print(' matrixC:', matrixC)
```

Run The code for addition of matrices using List Comprehension is very concise.

**Output**

```
matrixA: [[2, 1, 3], [3, 1, 5]]
+ matrixB: [[1, 4, 2], [5, 2, 0]]
-------------------------------
matrixC: [[3, 5, 5], [8, 3, 5]]
```

### Summary

In this tutorial of Python Examples, we learned how to do Matrix Addition in Python using For loop and List comprehension, with the help of well detailed examples.