Matrix in python is a two-dimensional data structure which is an array of arrays.
In python, you can easily create a matrix and access elements of the matrix. Here is a program to illustrate creating a matrix and different methods to print it.
Program to get matrix as input from user and print it in different type
t=()
rows=int(input("Enter number of Rows of the matrix : "))
cols=int(input("Enter number of columns of the matrix : "))
for i in range(rows):
c=()
for j in range(cols):
v=int(input("Enter Value : "))
c+=(v,)
t+=(c,)
print("Method 1")
for i in range(len(t)):
for j in range(len(t[i])):
print(t[i][j],end=' ')
print()
print("Method 2")
for R in t:
for C in R:
print(C,end = ' ')
print()
print("Method 3 ", end = ' ')
print(t)
Output:
Enter number of Rows of the matrix : 2
Enter number of columns of the matrix : 2
Enter Value : 54
Enter Value : 1
Enter Value : 76
Enter Value : 34
Method 1
54 1
76 34
Method 2
54 1
76 34
Method 3 ((54, 1), (76, 34))
Program to get matrix as input from user and print it in different type
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer