So in the previous lecture, we have learned about lists, and I told you that lists are nice for storing
some small amount of numbers or strings and for which we don't want to do any further mathematical calculations.
However, if we want to do mathematical calculations, which we will do a lot in this course, we will
have to use.
Arrays and arrays are not natively implemented in Python.
They are implemented in a module called Nampai.
So if you followed the previous lectures, we have already imported this.
So let me scroll up to the very top.
We have imported Nampai, S and P, and we need this to be able to use arrays because to define an array,
what we have to write is, for example, let me stored in the variable called array one.
We right and p dot array.
And then the syntax is normal brackets.
And then here we can write the square brackets one for nine, for example.
This would be our right.
So of course, let me call this.
So you see, the result would be an array which has these numbers.
So you may wonder, why do we have these double brackets here?
And the reason for this is that we also can define multidimensional arrays.
So let's redefine the two lists that we have defined previously also as arrays.
So we had here one, a three.
And here we had two lists that emerged inside each other.
So the first one one one two three and a second one was
this was one four nine.
Now you can see how the syntax works, so we need the normal brackets, basically.
Yeah, it's it's related to the NPRA commands.
And then here we have these two lists, you could say that emerged.
But since we have right here array, there are not lists anymore.
Now they are arrays.
We can call them, for example, like this.
And you see, this is already nicely organized in the output.
And it looks like a matrix from mathematics.
So, of course, we can now select individual elements like we did for lists so we can write every one
square bracket, one which gives us the second element since we're again start counting from zero.
And we can also select multiple of these.
So if we want to have the first two elements done, we get these two.
And here you realize if we just select a single number, then it's just a number.
And if we select multiple numbers, then it will remain the property of the array so that data type
will be on the right.
So if we do this for the second array, which would give us one as a number and a as a string, we have
two different data types.
This works, but gives us as an output, another option or property, which means something like a mixed
data type.
Now, if we want to select elements of a multi-dimensional array, which would, in our case, be the
array number three, this one here, then we can of course do this.
For example, if we want to have this property here, so we have to write zero and then to.
But so this is exactly the same as for this, but since we now have an array, we can also make it more
easy and just right like this is what exactly the same result.
So manipulating arrays is, of course, somewhat similar to this, but there are also important differences.
So there you cannot use anymore the syntax that we had previously.
Let me scroll up.
So we have previously list Dot Append, which is something that just works for lists that are natively
implemented in Python.
So here we must use a different commands and this command is called Nampai Dot Prepend.
So if we just use this command, Python doesn't know which area we want to manipulate.
So first, we must start with the array that we want to manipulate, and then we write a comma.
And the thing we want to add.
So this will be the new array.
But there's another important difference here previously for the lists, if we would write something
similar.
Then the list would now have been updated already here.
This is not the case.
If we right now, everyone, the old area appears.
This is because here we do not have lists anymore, but we have no idea.
So if we want to update the array, what we have to do is then write every one equals.
This one and now.
Sorry, I somehow switched to data time, I think.
Yes.
So now the one is updated, so of course, we can also use this append commands here for more general
purposes.
So, for example, we could now add to your true numbers at the same time, and we could do this by
adding or by appending voucher to see a whole array.
So we write and pray no more brackets.
Square brackets 5.6 now.
Oh, I did the same mistake again.
No one will be this.
So we have this hour and we have appended these two numbers.
So what we can also do is we can, of course, manipulate multidimensional areas.
So let's get back to our error number three, which is this matrix.
So basically, we have two arrays stored inside of one array.
So what we can do now is we can also append here some numbers or some errors so we can write and we
don't depend on a three comma and we right here what we want to append.
So it just will again be an array and the array will, for example, be one eight twenty seven, which
will be the cubic numbers of these.
So what happens if we run this?
Oh, this, this doesn't give us what we want.
It gives us one two three one four nine one eight twenty seven as a single one dimensional already.
So what I wanted to do here is add another line.
So in order to do this, we must use a bit of a different syntax.
So let me copy this and no change it.
So you see, the array number three starts with two square brackets, so we must now also start with
two square brackets so that these two arrays have the same rank.
And now it will still not work.
Still give the same result that we do not really want.
So we must tell it and option an additional property how we want to merge these two lists, or how we
want to append the array here to the other array.
And this will be axis equals zero, which means basically add or append this array on the first level.
So see row once again since first start counting from zero means first level, and then we will get
this nice three by three matrix.
Of course, we could also do this differently.
We could do this by adding a different row here instead.
For this, we would have to, for example, overwrite four and 16.
And now I think we have to right access one, which would be the second level on the way yet in horror.
Let me check what's the problem here?
Oh, yeah, right?
Because basically, if you see we want to add it here, so we must have here these these square brackets
as well.
So like this?
OK, so you see, manipulating multidimensional arrays can be a bit tricky because you have to really
take care of the syntax.
So you do not only need these arguments axis one and axis zero, but also you need to make sure that
these brackets here are correct.
By the way, I messed up here a bit.
This is, of course, not supposed to be math.
Is this supposed to be manipulated so we can just click here and move this to the bottom?
Yeah.
So now let's come to math.
This is just really quick.
This is where I want to show you where arrays are better than lists or more simple, so we can just
write, for example, here everyone times two.
And if you remember what everyone was, everyone.
Than this was one four, nine five five six.
And now we do not just repeat this error twice, but we really multiply every element of the array by
a number.
So this is really useful in a mathematical way.
And this was not possible for lists, if you remember.
So here was the same thing for lists.
So we had one three, four 10 and we got one three four 10 one three four 10.
So, of course, this also works with an addition, so we could write something like this.
So this time, every element will be added by the number two.
And of course, it also works with multidimensional arrays
with the multiplication and addition and also, of course, other operations.
So I hope you see that arrays can be quite advantageous.
So once again, this is useful when we want to do mathematical operations, sometimes we use arrays
instead of lists.
And also, these arrays require much less storage to store the individual elements compared to the lists.