def build_matrix(rows, cols): matrix = [] for r in range(0, rows): matrix.append([0 for c in range(0, cols)]) return matrix if __name__ == '__main__': build_matrix(6, 10)
Here is what the above code is Doing:
1. We start by creating an empty list called matrix.
2. We then use a for loop to iterate through each row in the matrix.
3. For each row, we create a list of 0s that is the same length as the number of columns.
4. We then append this list to the matrix list.
5. Finally, we return the matrix list.