Posts

Showing posts with the label python matrix program

Matrix input in python

Image
Get matrix input from user in Python A matrix is a rectangular arrangement of numbers arranged in rows and columns. Eg : 3x3 matrix          1  2  3 M =   4  5  6          7  8  9 In python a matrix is nothing but a list of lists (sub list). The above matrix can be written as [ [1,2,3], [4,5,6], [7,8,9] ]. Prerequisite :   List in python Method 1 : In this method, we get the elements of matrix and append it to a list and append the entire list to a main list (matrix). Code : #function to print matrix def printMatrix(matrix,r,c): for i in range(r): for j in range(c): print(matrix[i][j],  end=" ") print() #Get the number of rows from user r  = int(input("Enter the number of rows : ")) #Get the number of columns from the user c  = int(input("Enter the number of columns : ")) #initialize a matrix(list) matrix = [] for row in range(r): l = [] for col in range(c):                 #Get elements one by one num = int(input("Enter  element :