In the previous article, we have discussed Python Program to Check Whether a Matrix is a Scalar or Not

Given two matrixes and the task is to check if the given two matrixes are equal or not in Python.

**What is a matrix:**

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

**Example:**

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

**Examples:**

**Example1:**

**Input:**

Given first matrix : 5 2 0 0 5 0 0 0 5 Given second matrix : 5 2 0 0 5 0 0 0 5

**Output:**

The given first and second matrixes are equal.

**Example2:**

**Input:**

Given first matrix : 3 5 4 6 Given second matrix : 1 2 4 6

**Output:**

The given first and second matrixes are not equal.

## Program to Check Two Matrix are Equal or Not in Python

Below are the ways to check if the given two matrixes are equal or not in Python.

### Method #1: Using For Loop (Static Input)

**Approach:**

- Give the first matrix as static input and store it in a variable.
- Calculate the number of rows of the given first matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given first matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Give the second matrix as static input and store it in a variable.
- Take a variable say
**tempo**and initialize its value to zero. - Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Check if the condition firstmatrix[n][m] is not equal to the secondmatrix[n][m] using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- If the statement is true, then make the value of
**tempo**as 1. - Break the statement and exit the loop.
- Check if the value of tempo is equal to 1 using the if conditional statement.
- If the statement is true, then print “The given first and second matrixes are not equal.”
- Else print “The given first and second matrixes are equal.”
- The Exit of the Program.

**Below is the implementation:**

# Give the first matrix as static input and store it in a variable. fst_mtrx = [[5, 2, 0], [0, 5, 0], [0, 0, 5]] # Calculate the number of rows of the given first matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(fst_mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(fst_mtrx[0]) # Give the second matrix as static input and store it in a variable. scnd_mtrx = [[5, 2, 0], [0, 5, 0], [0, 0, 5]] # Take a variable say tempo and initialize its value to zero. tempo = 0 # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Check if the condition firstmatrix[n][m] is not equal to the secondmatrix[n][m] using # the if conditional statement where n is the iterator value of the parent For loop # and m is the iterator value of the inner For loop. if fst_mtrx[n][m] != scnd_mtrx[n][m]: # If the statement is true, then make the value of tempo as 1. tempo = 1 # Break the statement and exit the loop. break # Check if the value of tempo is equal to 1 using the if conditional statement. break if tempo == 1: # If the statement is true, then print "The given first and second matrixes are not equal." print("The given first and second matrixes are not equal.") else: # Else print "The given first and second matrixes are equal." print("The given first and second matrixes are equal.")

**Output:**

The given first and second matrixes are equal.

### Method #2: Using For loop (User Input)

**Approach:**

- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say
**gvnmatrix**. - Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to
**gvnmatrix**using the append() function. - Repeat the same for the second matrix.
- Take a variable say
**tempo**and initialize its value to zero. - Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Check if the condition firstmatrix[n][m] is not equal to the secondmatrix[n][m] using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- If the statement is true, then make the value of
**tempo**as 1. - Break the statement and exit the loop.
- Check if the value of tempo is equal to 1 using the if conditional statement.
- If the statement is true, then print “The given first and second matrixes are not equal.”
- Else print “The given first and second matrixes are equal.”
- The Exit of the Program.

**Below is the implementation:**

# Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. fst_mtrx = [] print("Enter elements of the first matrix :") # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. fst_mtrx.append(l) scnd_mtrx = [] print("Enter elements of the second matrix :") # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. scnd_mtrx.append(l) # Take a variable say tempo and initialize its value to zero. tempo = 0 # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Check if the condition firstmatrix[n][m] is not equal to the secondmatrix[n][m] using # the if conditional statement where n is the iterator value of the parent For loop # and m is the iterator value of the inner For loop. if fst_mtrx[n][m] != scnd_mtrx[n][m]: # If the statement is true, then make the value of tempo as 1. tempo = 1 # Break the statement and exit the loop. break # Check if the value of tempo is equal to 1 using the if conditional statement. break if tempo == 1: # If the statement is true, then print "The given first and second matrixes are not equal." print("The given first and second matrixes are not equal.") else: # Else print "The given first and second matrixes are equal." print("The given first and second matrixes are equal.")

**Output:**

Enter some random number of rows of the matrix = 2 Enter some random number of columns of the matrix = 2 Enter elements of the first matrix : Enter {2} elements of row {1} separated by spaces = 3 5 Enter {2} elements of row {2} separated by spaces = 4 6 Enter elements of the second matrix : Enter {2} elements of row {1} separated by spaces = 1 2 Enter {2} elements of row {2} separated by spaces = 4 6 The given first and second matrixes are not equal.

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.