print output of matrix 3x3 in 3 lines in python

To print the output of a 3x3 matrix in 3 lines in Python, you can use nested loops. Here's the code:

main.py
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for row in matrix:
    for element in row:
        print(element, end=" ")
    print()
131 chars
7 lines

Output:

main.py
1 2 3 
4 5 6 
7 8 9 
21 chars
4 lines

In this code, we iterate over each row of the matrix using the outer loop, and then iterate over each element in each row using the inner loop. The end=" " argument in the print statement ensures that the elements are printed on the same line with a space as a separator. The print() statement after the inner loop is used to move to the next line after printing each row.

related categories

gistlibby LogSnag