Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 18 Sep, 12:44:14

Here is an example of a Matrix class for 17-year-old pupils:

```python class Matrix: def init(self, rows, columns): self.rows = rows self.columns = columns self.data = [[0] * columns for _ in range(rows)]

def set_value(self, row, column, value):
    if row < 0 or row >= self.rows or column < 0 or column >= self.columns:
        raise IndexError("Invalid row or column index")
    self.data[row][column] = value

def get_value(self, row, column):
    if row < 0 or row >= self.rows or column < 0 or column >= self.columns:
        raise IndexError("Invalid row or column index")
    return self.data[row][column]

def print_matrix(self):
    for row in self.data:
        print(row)

Example usage:

matrix = Matrix(3, 3) matrix.set_value(0, 0, 1) matrix.set_value(0, 1, 2) matrix.set_value(0, 2, 3) matrix.set_value(1, 0, 4) matrix.set_value(1, 1, 5) matrix.set_value(1, 2, 6) matrix.set_value(2, 0, 7) matrix.set_value(2, 1, 8) matrix.set_value(2, 2, 9)

matrix.print_matrix() ```

This Matrix class allows you to create a matrix with a specified number of rows and columns. You can set values at specific positions in the matrix using the set_value method, and retrieve values using the get_value method. The print_matrix method prints the matrix in a readable format.