# Python arrays without numpy!

**URL:** <https://forum.freecodecamp.org/t/python-arrays-without-numpy/428868>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [November 5, 2020, 7:10am UTC](https://forum.freecodecamp.org/t/python-arrays-without-numpy/428868 "2020-11-05T07:10:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saad-coder](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Saad-coder](https://forum.freecodecamp.org/u/Saad-coder)\
**Post date:** [November 5, 2020, 7:10am UTC](https://forum.freecodecamp.org/t/python-arrays-without-numpy/428868/1 "2020-11-05T07:10:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mukeshgurpude](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mukeshgurpude/32/180919_2.png) [@mukeshgurpude](https://forum.freecodecamp.org/u/mukeshgurpude)\
**Post date:** [November 5, 2020, 7:50am UTC](https://forum.freecodecamp.org/t/python-arrays-without-numpy/428868/2 "2020-11-05T07:50:35Z")

</div>

Basically you’re talking about [Operator Overloading](https://en.wikipedia.org/wiki/Operator_overloading),  
In python we can implement ` __sub__ ` method for subtraction ` __add__ ` for addition ` __mul__ ` for multiplication etc…  
Read more [here](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types)

---

<div class="post-metadata">

**Author:** ![thirty\_two](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@thirty\_two](https://forum.freecodecamp.org/u/thirty_two)\
**Post date:** [November 6, 2020, 5:24am UTC](https://forum.freecodecamp.org/t/python-arrays-without-numpy/428868/3 "2020-11-06T05:24:56Z")

</div>

You can find a complete example here

> <https://github.com/TheAlgorithms/Python/blob/master/matrix/matrix_class.py>

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

```auto
    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

```auto
>>> 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.]]

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:29pm UTC](https://forum.freecodecamp.org/t/python-arrays-without-numpy/428868/4 "2021-06-01T18:29:07Z")

</div>


