{"id":19479,"date":"2021-08-30T16:06:32","date_gmt":"2021-08-30T10:36:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=19479"},"modified":"2021-11-22T18:36:30","modified_gmt":"2021-11-22T13:06:30","slug":"python-program-to-read-and-display-a-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-read-and-display-a-matrix\/","title":{"rendered":"Python Program to Read and Display a Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to find the Sum of Series 1^1+2^2+3^3…+N^N<\/a> What is a matrix:<\/strong><\/p>\n 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.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/p>\n Below are the ways to read and display the elements of the given matrix in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nGiven a matrix, the task is to scan and print the elements of the given matrix in Python<\/p>\nProgram to Read and Display a Matrix in Python<\/h2>\n
\n
Method #1: Using list Method<\/h3>\n
\n
# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n\r\n mtrx.append(l)\r\n\r\n# To print all the elements of the given matrix.\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of rows using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Print the element of the matrix by printing gvnmatrix[n][m] value\r\n # where n is the iterator value of the parent For loop and m is the iterator\r\n # value of the inner For loop.\r\n print(mtrx[n][m], end=' ')\r\n print()\r\n<\/pre>\n
Enter some random number of rows of the matrix = 3\r\nEnter some random number of columns of the matrix = 4\r\nEnter {4} elements of row {1} separated by spaces = 5 8 3 7\r\nEnter {4} elements of row {2} separated by spaces = 5 9 7 2\r\nEnter {4} elements of row {3} separated by spaces = 1 6 8 5\r\n5 8 3 7 \r\n5 9 7 2 \r\n1 6 8 5<\/pre>\n
Method #2: Using List Comprehension<\/h3>\n
\n
# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using the list Comprehension\r\n l=[int(eleme) for eleme in input('Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()]\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n mtrx.append(l)\r\n\r\n# To print all the elements of the given matrix.\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of rows using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Print the element of the matrix by printing gvnmatrix[n][m] value\r\n # where n is the iterator value of the parent For loop and m is the iterator\r\n # value of the inner For loop.\r\n print(mtrx[n][m],end= ' ')\r\n print()<\/pre>\n
Enter some random number of rows of the matrix = 4\r\nEnter some random number of columns of the matrix = 5\r\nEnter {5} elements of row {1} separated by spaces = 1 2 3 4 5\r\nEnter {5} elements of row {2} separated by spaces = 7 8 5 9 4\r\nEnter {5} elements of row {3} separated by spaces = 1 11 25 7 6\r\nEnter {5} elements of row {4} separated by spaces = 2 5 3 4 6\r\n1 2 3 4 5 \r\n7 8 5 9 4 \r\n1 11 25 7 6 \r\n2 5 3 4 6<\/pre>\n
\n