Python arrays without numpy!

Can someone help me regarding the subtraction and multiplication of two matrices which I created using arrays (without numpy) and I am doing it using object oriented by making class and functions. I had created 2 matrices and print them by calling the class in objects and now I have to make a function in the same class which subtracts and another function which multiplies these 2 matrices.

Any help?

Basically you’re talking about Operator Overloading,
In python we can implement `__sub__` method for subtraction `__add__` for addition `__mul__` for multiplication etc…
Read more here

You can find a complete example here

Basically, you want to overload the arithmetic operators, for example

``````    def __add__(self, other):
if self.order != other.order:
raise ValueError("Addition requires matrices of the same order")
return Matrix(
[
[self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)]
for i in range(self.num_rows)
]
)
``````

So, you can now do, assuming you have already implemented your basic Matrix class

``````>>> rows = [
...     [1, 2, 3],
...     [4, 5, 6],
...     [7, 8, 9]
... ]
>>> matrix = Matrix(rows)
>>> matrix2 = matrix * 3
>>> print(matrix + matrix2)
[[4. 8. 12.]
[16. 20. 24.]
[28. 32. 36.]]
``````