Example HTML page

# A Python program to perform arithmetic operations on two matrices.

To represent a matrix we use the concept of nested lists. The elements in both the input matrices are represented as linked lists. To store the result the output list is initialized to zero. When we iterate the two matrices (Here M1 and M2 are the two chosen matrices) by specific operation then the result of the corresponding elements in the matrices are assigned to the respected positions in the output matrix. Finally we will be displayed the desired output. To perform arithmetic operations like +,-,* and / the number of elements in both the matrices or length of both the matrices should be equal.

For 2X2 matrix

```M1 = [[1, 2],[4, 1]]
M2 = [[3, 2],[1, 2]]
for i in range(len(M1)):
for j in range(len(M1)):
print(num)

```

We can also write the same above code as follows; not only for addition but also for subtraction, multiplication and division operations. But we will get same output for both formats.

```M1 = [[1, 2],
[4, 1]]
M2 = [[3, 2],
[1, 2]]
[0, 0]]
for i in range(len(M1)):
for j in range(len(M1)):
print(num)
```

Output

 [4, 4] [5, 3]

#Subtraction of two matrices

```M1 = [[1, 2],[4, 1]]
M2 = [[3, 2],[1, 2]]
sub=[[0, 0],[0, 0]]
for i in range(len(M1)):
for j in range(len(M1)):
sub[i][j] = M1[i][j] - M2[i][j]
for num in sub:
print(num)

```

Output

 [-2, 0] [3, -1]

#Multiplication of two matrices

```M1 = [[1, 2],[4, 1]]
M2 = [[3, 2],[1, 2]]
mul=[[0, 0],[0, 0]]
for i in range(len(M1)):
for j in range(len(M1)):
mul[i][j] = M1[i][j] * M2[i][j]
for num in mul:
print(num)

```

Output

 [3, 4] [4, 2]

#Division of two matrices

```M1 = [[1, 2],[4, 1]]
M2 = [[1, 2],[1, 2]]
div=[[0, 0],[0, 0]]
for i in range(len(M1)):
for j in range(len(M1)):
div[i][j] = M1[i][j] / M2[i][j]
for num in div:
print(num)

```

Output

 [1.0, 1.0] [4.0, 0.5]

For 3×3 matrix

```M1 = [[1, 1, 1],[1, 1, 1],[1, 1, 1]]
M2 = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
sum=[[0, 0, 0],[0, 0, 0],[0, 0, 0]]
for i in range(len(M1)):
for j in range(len(M1)):
sum[i][j] = M1[i][j] + M2[i][j]
for num in sum:
print(num)

```

Output

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

Let you try the following:

1. Write python scripts to perform the remaining operations like subtraction, multiplication and division on 3×3 matrices.

Example HTML page