In [39]:
def multiplyMatrices(a, b):
    if len(a[0]) != len(b):
        raise Exception("Incompatible Matrices")
    else:
        rows = len(a) # rows of a
        columns = len(b[0]) # columns of b
        Matrix = [[0 for x in range(columns)] for y in range(rows)]
        # printMatrix("initial resulting matrix", Matrix)
        for i in range(rows):
            for j in range(columns):
                for k in range(len(a[0])):
                    Matrix[i][j] = Matrix[i][j] + a[i][k] * b[k][j]
        return Matrix

In [24]:
def printMatrix(msg, a):
    print (msg)
    for x in range(len(a)):
        for y in range(len(a[0])):
            print (a[x][y], end=' ')
        print()

In [42]:
def getDummy2x2MatrixA():
    a = [[0 for x in range(2)] for y in range(2)]
    a[0][0] = 2
    a[1][0] = 5
    a[0][1] = 1
    a[1][1] = 2
    return a

def getDummy1x2Matrixb():
    a = [[0 for x in range(2)] for y in range(1)]
    a[0][0] = 2
    a[0][1] = 1
    return a

a = getDummy2x2MatrixA()
b = getDummy1x2Matrixb()

print("These 2 matrices should not be compatible")
printMatrix("a: rows= %s columns= %s" % (len(a),len(a[0])),a)
printMatrix("b: rows= %s columns= %s" % (len(b),len(b[0])),b)

multiplyMatrices(a, b)


These 2 matrices should not be compatible
a: rows= 2 columns= 2
2 1 
5 2 
b: rows= 1 columns= 2
2 1 
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-42-0c88ff0022fc> in <module>()
     20 printMatrix("b: rows= %s columns= %s" % (len(b),len(b[0])),b)
     21 
---> 22 multiplyMatrices(a, b)

<ipython-input-39-d97b53005970> in multiplyMatrices(a, b)
      1 def multiplyMatrices(a, b):
      2     if len(a[0]) != len(b):
----> 3         raise Exception("Incompatible Matrices")
      4     else:
      5         rows = len(a) # rows of a

Exception: Incompatible Matrices

In [43]:
def get2x2MatrixA():
    a = [[0 for x in range(2)] for y in range(2)]
    a[0][0] = 2
    a[1][0] = 5
    a[0][1] = 1
    a[1][1] = 2
    return a

def get2x2MatrixB():
    b = [[0 for x in range(2)] for y in range(2)]
    b[0][0] = 1
    b[1][0] = 3
    b[0][1] = 4
    b[1][1] = 6
    return b

a = get2x2MatrixA()
b = get2x2MatrixB()

print("These 2 matrices should be compatible")
printMatrix("a: rows= %s columns= %s" % (len(a),len(a[0])),a)
printMatrix("b: rows= %s columns= %s" % (len(b),len(b[0])),b)

c = multiplyMatrices(a, b)
printMatrix("c: rows= %s columns= %s" % (len(c),len(c[0])),c)


These 2 matrices should be compatible
a: rows= 2 columns= 2
2 1 
5 2 
b: rows= 2 columns= 2
1 4 
3 6 
c: rows= 2 columns= 2
5 14 
11 32 

In [40]:
def get2X3Matrix():
    matrix = [[0 for x in range(3)] for y in range(2)]
    matrix[0][0] = 1
    matrix[1][0] = 2

    matrix[0][1] = 2
    matrix[1][1] = 3
    
    matrix[0][2] = 3
    matrix[1][2] = 4
    return matrix

def get3X1Matrix():
    matrix = [[0 for x in range(1)] for y in range(3)]
    matrix[0][0] = 2
    matrix[1][0] = 1
    matrix[2][0] = 3
    return matrix

a = get2X3Matrix()
b = get3X1Matrix()

print("These 2 matrices should be compatible")
printMatrix("a: rows= %s columns= %s" % (len(a),len(a[0])),a)
printMatrix("b: rows= %s columns= %s" % (len(b),len(b[0])),b)

c = multiplyMatrices(a, b)
printMatrix("c: rows= %s columns= %s" % (len(c),len(c[0])),c)


These 2 matrices should be compatible
a: rows= 2 columns= 3
1 2 3 
2 3 4 
b: rows= 3 columns= 1
2 
1 
3 
c: rows= 2 columns= 1
13 
19 

In [ ]: