Python Program to Multiply Two MatricesIntroduction:In this tutorial, we will discuss a Python program to multiply two matrices. We will write a Python program to get the multiplication of two input matrices and print the result in output. This Python program specifies how to multiply two matrices having specific values. Before writing the Python program, let's first look at the overview of the multiplication of two matrices. Matrix multiplicationMatrix multiplication is a binary operation that uses a pair of matrices to produce another matrix. The elements within the matrix are multiplied according to elementary arithmetic. In the multiplication of two matrices, the row elements of the first matrix are multiplied by the column elements of the second matrix. In matrix multiplication row value of the 1st matrix must be the same as the column value of the 2nd matrix. That means if 1st matrix is 3X3, then 2nd matrix column value must be 3. Example: Suppose we have given following two A and B matrices: C would be the addition of above given two matrices, i.e., C = A+B, and therefore C should be: As we can see that the resulting matrix C, which is also known as matrix product, has the same number of rows as the first matrix (A matrix) and the same number of columns as the second matrix (B matrix). We also know this type of multiplication of matrices as dot product of matrices. Multiplication of two matricesNow, we will write a Python program for the multiplication of two matrices where we perform the multiplication as we have performed in the abovegiven example. We can use various methods to write a Python program like this, but in this tutorial, we will only use the following two methods:
In both methods, we will write an example program to understand their implementation for multiplying two matrices. Method 1: Using nested for loop method:In this method, we are going to use nested for loop on two matrices and perform multiplication on them and store multiplication result in the third matrix as the result value. Let's understand implementation of this method through the following example. Program code 1: Now we give an example of matrix multiplication using nested for loop in Python. The program code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The multiplication result of matrix A and B is: [37, 43, 59] [34, 58, 62] [58, 92, 103] Program code 2: Now we give an example of matrix multiplication using nested for loop in Python. The program code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  [3, 2, 1] [6, 5, 4] [9, 8, 7] Method 2: Using nested list comprehension method:In this method, we will use nested list comprehension to get the multiplication result of two input matrices. While using the list comprehension method in the program, we will also use 'zip in Python' on the nested list. Let's understand the implementation of this method through the following example. Program code: Now we give an example of matrix multiplication using Python's nested list comprehension method. The program code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The multiplication result of matrix A and B is: [3, 2, 1] [6, 5, 4] [9, 8, 7] Conclusion:In this article, we are discussing a Python program to multiply two matrices. Here we talk about two types of matrix multiplication in Python and give some suitable examples.
Next TopicPyhton Transpose Matrix
