belongs to collection: Python Array Programs
There is no specific data type in Python to create a matrix, we can use list of list to create a matrix.
Consider the below example,
mat = [ [10, 20, 30], [40, 50, 60], [70, 80, 80] ]
It can be considered a 3x3 matrix, there are 3 rows and 3 columns in 'mat' matrix.
Just like matrix in C/C++, we can access the elements in Python also.
Consider the below program,
# Python matrix creation mat = [ [10, 20, 30], [40, 50, 60], [70, 80, 80] ] # printing the matrix print("mat: ", mat) # printing rows print("mat[0]: ", mat[0]) print("mat[1]: ", mat[1]) print("mat[2]: ", mat[2]) # printing specific elements print("mat[0][0]: ", mat[0][0]) print("mat[0][1]: ", mat[0][1]) print("mat[0][2]: ", mat[0][2]) print("mat[1][0]: ", mat[1][0]) print("mat[1][1]: ", mat[1][1]) print("mat[1][2]: ", mat[1][2]) print("mat[2][0]: ", mat[2][0]) print("mat[2][1]: ", mat[2][1]) print("mat[2][2]: ", mat[2][2]) # printing matrix using loop (matrix form) print("Matrix is: ") for i in range(3): for j in range(3): print(mat[i][j], end = " ") print() # prints new line
Output
mat: [[10, 20, 30], [40, 50, 60], [70, 80, 80]] mat[0]: [10, 20, 30] mat[1]: [40, 50, 60] mat[2]: [70, 80, 80] mat[0][0]: 10 mat[0][1]: 20 mat[0][2]: 30 mat[1][0]: 40 mat[1][1]: 50 mat[1][2]: 60 mat[2][0]: 70 mat[2][1]: 80 mat[2][2]: 80 Matrix is: 10 20 30 40 50 60 70 80 80
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Consider the below example,
It can be considered a 3x3 matrix, there are 3 rows and 3 columns in 'mat' matrix.
Accessing matrix elements
Just like matrix in C/C++, we can access the elements in Python also.
Consider the below program,
Output
need an explanation for this answer? contact us directly to get an explanation for this answer