0
Q:

matrix in python3

columns = []
grid = []

num_of_rows = 3
num_of_columns = 4

#creates the matrix
for x in range(0, num_of_rows): # 3 rows by 4 columns
    for y in range(0, num_of_columns): 
        columns.append((x, y))
    grid.append(columns.copy())
    columns = []

#prints the grid in the matrix format
for x in range(0, len(grid)):
    print(grid[x])
    
#output:
# [(0, 0), (0, 1), (0, 2), (0, 3)]
# [(1, 0), (1, 1), (1, 2), (1, 3)]
# [(2, 0), (2, 1), (2, 2), (2, 3)]
1
# 5x6, 2-d maxtrix of booleans using list comprehension:

matrix = [[False for col in range(6)] for row in range(5)]

# 6x5, 2-d matrix of banana's using list comprehension:

matrix = [['banana' for col in range(5)] for row in range(6)]
0

New to Communities?

Join the community