New course! Every coder should learn Generative AI!
Try a free lesson+ 8
import numpy as np
matrix = np.arange(1,10).reshape(3,3)
+ 5
R = int(input("enter the number of rows :"))
C = int(input("enter the number ofcolumns :"))
matrix = [ ]
print ("enter the entries row wise:")
for i in range(R):
a = [ ]
for j in range(C):
a. append(int(input()))
matrix. append(a)
for i in range(R):
for j in range(C):
print (matrix[i][j], end = " " )
print()
+ 4
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
or
matrix = [[j + i*3 + 1 for j in range(3)] for i in range(3)]
+ 2
generating a matrix
n = [int(i) for i in input().split()]
matrix = [n[i:i+3] for i in range(0,len(n),3)]
+ 2
a=0
for i in range(1,4):
for j in range(1,4):
a+=1
print(a,end="")
print("")
+ 1
If your matrix stores same type, use numpy for it.
It’s efficient!
On the other hand, use nest lists to create matrix. But it’s not common.
Personal recommendation: numpy, pandas.
These two libraries are popular and efficient compared with Python list or dict because of their structure.
Good luck!