How to check whether a matrix is a square matrix in Python

1 Answer

0 votes
def is_square(matrix):
    # Return True if the matrix is square (rows == columns).
    return all(len(row) == len(matrix) for row in matrix)

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

print("The matrix is a square matrix." if is_square(matrix)
            else "The matrix is not a square matrix.")


"""
run:

The matrix is a square matrix.

"""

 



answered 1 day ago by avibootz
...