How to build a matrix in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to build a matrix in python?

Declare a (n x n) matrix.

19th Jul 2017, 3:43 PM
Deep chand
Deep chand - avatar
4 Answers
+ 8
You can use the numpy module for that. It supports a matrix class. import numpy as np mx = np.matrix([[1,3],[8,7]]) print(mx) >>> [[1 3] [8 7]]
19th Jul 2017, 5:33 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
def a(n,m): matrix = [[i for i in range(n)] for j in range(m)] return matrix
19th Jul 2017, 4:08 PM
Maya
Maya - avatar
+ 3
All matrices (I believe) can be implemented using 2D arrays so yeah, you can try that
19th Jul 2017, 4:44 PM
Garikai
Garikai - avatar
0
@Kuba Siekierzynski I need to declare a square matrix of order n and accept values in run time.
20th Jul 2017, 11:56 AM
Deep chand
Deep chand - avatar