make the matrix in python

In Python, a matrix can be represented as a nested list of lists. Each list represents a row in the matrix, and each element within that list is an element within the matrix. Here is an example code snippet to create a 3x3 matrix with all zeros:

main.py
matrix = [[0, 0, 0],
          [0, 0, 0],
          [0, 0, 0]]

print(matrix)
78 chars
6 lines

This will output the following matrix:

main.py
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
36 chars
4 lines

To access and modify elements in the matrix, you can use the row and column indices, just like accessing elements in a nested list:

main.py
# accessing element at row 0, column 1
element = matrix[0][1]
print(element)

# modifying element at row 1, column 2
matrix[1][2] = 1
print(matrix)
148 chars
8 lines

This will output:

main.py
0
[[0, 0, 0],
 [0, 0, 1],
 [0, 0, 0]]
38 chars
5 lines

related categories

gistlibby LogSnag