[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
2. Print the element 8.
3. Print the second row of the matrix, i.e. 4, 5 and 6.
4. Print the third column of the matrix, i.e. 3, 6 and 9.
5. Print the diagonal of the matrix, i.e., 1, 5 and 9.
Right Answer:
# Step 1
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Step 2
print(matrix[2][1]) # Output: 8
# Step 3
print(matrix[1]) # Output: [4, 5, 6]
# Step 4
third_column = [row[2] for row in matrix]
print(third_column) # Output: [3, 6, 9]
# Step 5
diagonal = [matrix[i][i] for i in range(len(matrix))]
print(diagonal) # Output: [1, 5, 9]