VisuAlg Web

Tabelas e Grades Bidimensionais

Matrizes em Python são geralmente listas de listas, acessadas como mat[linha][coluna] .

Matrizes em Python são geralmente listas de listas, acessadas como mat[linha][coluna].

Exemplo de códigoPython
# 2x2 matrix in Python (list of lists)
matrix = [
[10, 20],
[30, 40]
]

# In Python, indexes start at 0
print("The value at row 2, column 1 is:", matrix[1][0])
Exemplo de códigoPython
# Creates a 3x3 matrix initialized with zeros
m = [[0] * 3 for _ in range(3)]

# Filling the matrix with numbers based on positions
def show_matrix():
for l in range(3):
for c in range(3):
m[l][c] = l + c
print(f"[{m[l][c]:2}]", end=" ")
print()

show_matrix()