Python Program to Read and Display a Matrix

In the previous article, we have discussed Python Program to find the Sum of Series 1^1+2^2+3^3…+N^N
Given a matrix, the task is to scan and print the elements of the given matrix 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.

Program to Read and Display a Matrix in Python

Below are the ways to read and display the elements of the given matrix in Python:

Method #1: Using list Method

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.
  • To print all the elements of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Print the element of the matrix by printing gvnmatrix[n][m] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • 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.
mtrx = []
# 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.

    mtrx.append(l)

# To print all the elements of the given matrix.
# 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 rows using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Print the element of the matrix by printing gvnmatrix[n][m] value
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        print(mtrx[n][m], end=' ')
    print()

Output:

Enter some random number of rows of the matrix = 3
Enter some random number of columns of the matrix = 4
Enter {4} elements of row {1} separated by spaces = 5 8 3 7
Enter {4} elements of row {2} separated by spaces = 5 9 7 2
Enter {4} elements of row {3} separated by spaces = 1 6 8 5
5 8 3 7 
5 9 7 2 
1 6 8 5

Method #2: Using List Comprehension

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 Comprehension.
  • Add the above row elements list to gvnmatrix using the append() function.
  • To print all the elements of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Print the element of the matrix by printing gvnmatrix[n][m] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • 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.
mtrx = []
# 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 Comprehension
    l=[int(eleme) for eleme in 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.
    mtrx.append(l)

# To print all the elements of the given matrix.
# 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 rows using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Print the element of the matrix by printing gvnmatrix[n][m] value
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        print(mtrx[n][m],end= ' ')
    print()

Output:

Enter some random number of rows of the matrix = 4
Enter some random number of columns of the matrix = 5
Enter {5} elements of row {1} separated by spaces = 1 2 3 4 5
Enter {5} elements of row {2} separated by spaces = 7 8 5 9 4
Enter {5} elements of row {3} separated by spaces = 1 11 25 7 6
Enter {5} elements of row {4} separated by spaces = 2 5 3 4 6
1 2 3 4 5 
7 8 5 9 4 
1 11 25 7 6 
2 5 3 4 6

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.