I don't understand this code at all, but here's some tips...
For-loops in Python look like this (this is Python shell):
>>> for i in range(1, 6+1):
... print(i)
...
1
2
3
4
5
6
Not knowing Mupad I am unsure if for n from 1 to 6 do
means:
a) 0, 1, 2, 3, 4, 5 (range(0, 6)
)
b) 0, 1, 2, 3, 4, 5, 6 (range(0, 6+1)
)
c) 1, 2, 3, 4, 5, 6 (range(1, 6+1)
)
I see that there's references of "matrix", for that see the commonly used library NumPy and their documentation on numpy.matrix. It already has a lot of methods for your simplicity.
Your best option is probably to search up the NumPy alternative rather than do the work with this piece of code.