How to Display a Matrix with Positive and Negative Values in Python
Date: 2023-03-29 11:30:51
Here is an example of how to display a matrix with both negative and positive values in Python:
matrix = [[1, 2, -3], [-4, 5, 6], [7, -8, 9]]
for row in matrix:
for val in row:
print(f'{val:4}', end='')
print()
Output:
1 2 -3
-4 5 6
7 -8 9
In this example, we create a matrix with three rows and three columns, and some of the values are negative. Then we use a nested for loop to iterate through each row and each value in the matrix, and print each value with a width of 4 characters, using a formatted string (f'{val:4}'
). We end each row with print()
to move to the next line.