1# A basic code for matrix input from user
2
3R = int(input("Enter the number of rows:"))
4C = int(input("Enter the number of columns:"))
5
6# Initialize matrix
7matrix = []
8print("Enter the entries rowwise:")
9
10# For user input
11for i in range(R): # A for loop for row entries
12 a =[]
13 for j in range(C): # A for loop for column entries
14 a.append(int(input()))
15 matrix.append(a)
16
17# For printing the matrix
18for i in range(R):
19 for j in range(C):
20 print(matrix[i][j], end = " ")
21 print()
1If you want to take n lines of input where each line contains m space separated integers like:
2
31 2 3
44 5 6
57 8 9
6
7a=[] // declaration
8for i in range(0,n): //where n is the no. of lines you want
9 a.append([int(j) for j in input().split()]) // for taking m space separated integers as input
1matrix = [input().split() for i in range(no_of_rows)] # if only row is given and the number of coloumn has to be decide by user
2matrix= [[input() for j in range(no_of_cols)] for i in range(no_of_rows)] # if both row and coloumn has been taken as input from user
1# Taking one row at a time with space-separated values.
2# And converting each of them to using map and int function.
3
4matrix = []
5for i in range(row):
6 col = list(map(int, input().split()))
7 matrix.append(col)
8print(matrix)
9
10'''
11Input :
121 2
133 4
14Output :
15[[1, 2], [3, 4]]
16'''
1row=int(input("Enter number of rows you want: "))
2col=int(input("Enter number of columns you want: "))
3mat=[]
4for m in range(row):
5 a=[]
6 for n in range(col):
7 a.append(0)
8 mat.append(a)
9
10for i in range(len(mat)):
11 for j in range(len(mat[0])):
12 mat[i][j]=int(input("Input element: "))
13print("Your Matrix is :",mat)