To add two matrices in python, you have to choose two matrix and another empty matrix. The third or empty matrix is used to store the addition result of the two matrices as shown in the program given below.
Following python program adds two matrices to print the addition result of the two matrices:
# Python Program - Add Two Matrices matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] matrix2 = [[10, 11, 12], [13, 14, 15], [16, 17, 18]] rmatrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(len(matrix1)): for j in range(len(matrix1)): rmatrix[i][j] = matrix1[i][j] + matrix2[i][j] for r in rmatrix: print(r)
Here is the sample run of the above Python program shows how to add two matrices:
You may also like to learn or practice the same program in other popular programming languages: