Hello, welcome back to the next session in Python programming course. We are in module 3 currently. We are talking about how to manage arrays in Python using two different modules, array module and numpy module. We are currently discussing numpy module in which we had seen what is a numpy module, then how do you install and use it in your application, how do you create a using numpy module and what is a zero dimensional one dimensional two dimensional and three dimensional arrays then how do you find out the dimensions of an array then we talked about how we can initialize an array using three different methods like ones zeros and random number In this video we are going to talk about the array indexing.
So we access an array element by referring to its index number. All of you are very much familiar with this indexing. In your C, C++ and Java program we use the index like this. If it is one dimensional array there is going to be only one index. If it is two dimensional array we will have two indices.
If it is three dimensional array obviously we will have three dimension. So this is going to be the first dimension. This is the second dimension. And this is going to be the third dimension. Right.
So for the two dimensional array, we will have rows and columns. Whereas for the three dimensional array, three different axis will be there. Axis 0, axis 1 and axis 2. So if it is going to be 1, 1, 1, then it is going to be the array index 1. So that means the second row.
Second row. example from the axis 0 it is going to be the second one right because this is 0 and this is 1. So this is 1 and in axis 1 again this is going to be this particular point and in axis 2 again this is going to be this particular point. So this will be taking value 4 from this three dimensional array right. So this row this one and here this particular row right actually whatever element is below this 4 will be taken sorry whatever element below this 4 you will have one particular value right so which is parallel to this 0 so that will be taken as array of 1 comma 1 comma 1 right so this is how indexing will be done in array python arrays and not only that negative indexing can also be used to add access array elements from the end. We are already familiar with this because for list we had already used how negative indexing can be used to access the list elements from the end.
In the same way negative indexing can be used to access array elements from the end. Let us see an example. Okay, so this is the kind of example, all the examples here will help you to understand how the values can be accessed from the end. Please observe here, we have A two-dimensional array, we have a two-dimensional array created which we call as AR and that is why I am using all two indices here because it is a two-dimensional array, right.
First row, is having 1, 2, 3, 4, 5. Second row is having 6, 7, 8, 9, 10. Third row is having values 11, 12, 13, 14, 15. Okay. Now if you give 0 comma minus 1, it will take take the last element from the first row. Why?
The index 0 specifies it is the first row and this negative index says it must take the last element. So what is the first row last element? It is going to be 5. So you can see the output is 5. And if you give minus 1 comma 2, minus 1 comma 2. comma 2. So minus 1 indicates it must be the last row.
Which is the last row? This is the last row of the two-dimensional array. Then second value right which is the second value and if you take this value 1. it is not second actually it is the index right.
So index position is 2. So it is index position 0, this is index position 1 and this is index position 2. So minus 1 comma 2 will take value 30 that is the third element from the last row. Then if you give minus 1 minus 1 it will take last element from the last row right. So minus 1 says it is the last row. So this is the last row and in last row you want to take the last element because the index is negative.
So you start from the end. So the last element is going to be 50. So last element from last row is true. Then if I give 1 comma minus 1. So 1 indicates it is going to be the second row.
This is the first row 0. This is the second row 1. So I am going to take the second row. In the second row it must be the last element. What is the second row last element? It is 10. So you get the value 10. And if you observe I am going to take second row.
element from the last row, second element from the last row and you observe, so you just find out the difference here. Here it is 1 comma minus 1, so 1 says it is the second row, minus 1 says it must be the last element but here it is minus 1 comma 1, minus 1 will say it is the last row and 1 will say it is the second element, right. So, the second element from the last row, row is 12 and if you give minus 3 comma minus 2 both negative values minus 3 comma minus 2 right.
So when I say minus 3 the last element is last row is minus 1 and this one will be minus 2 and this one will be minus 3. So minus 3 will take the first row here then in that I want to have minus 2 second last element. So this is the last element and second last element is 4 that is how it is taken and here it is minus 2 comma minus 2 again second last element from second row right. So what is the second row here minus this is minus 1 and this is minus 2. So this minus 2 indicates the second row and in the second row I want the second last element because last element will be represented as minus 1. second last element is going to be 9 the same way if it is 2 comma minus 1 2 is the third row 0 1 2 this is 0 this is 1 this is 2 so third row minus 1 last element is 15 then if I give minus 1 comma 4 minus 1 comma 4 minus 1 means it is the last row which is the last row here so this is the last row minus 1 It is going to be 15. And if I give minus 1 comma minus 2, minus 1 is the last row.
Minus 2 is going to be the second last element which will be the value 14. So this is how the negative indexing can be used to access elements from the last or from the end of an array. Next let us see what is array slicing. So we have seen now array index. Next see what is array slicing.
Slicing in Python means accessing elements from one given index to another given index. We are going to give a range here. From that particular range you are going to take the elements. which you call as slicing in Python. Range of values, starting index and the ending index will be given to access the elements which you call as slicing.
And if you want to do slicing, How do you specify it? You have to specify the starting index and you have to specify the ending index also. And sometimes you may want to give step also. So you want to take the first element, then you want to take the third element, then fifth element and so on. Or sometimes you want to start with the second element.
then fourth element then sixth element like that so by the time you have to define the step also so to define the step you have to give the starting index ending index then you have to give the step value also right so we just something like the increment and if you have not given any value for the starting index then it will be considered as 0. So right from the index 0 it will be used right like this. If you are not giving any starting index but you have given some value for end index as well as for the step. Or you might have given only the ending index and no value for the starting index.
Then here the starting index will be considered as 0. The same way if you have not given any value to the end then it will be considered as the length of the array. Say for example I give the starting index. index here but no ending index is specified but I give a step value or sometimes you give just the starting index and you do not give any ending index. In that case the data will be accessed till the end of the array or the length of the array will be considered as the end value and sometimes you may not give any value for the step.
that is the case then it will be incrementing the array index by 1. So by default it is always 1. So you give just starting index and the ending index. You do not give any step value. By the time the step value will be considered as 1. So this is how the slicing indices can be specified for accessing the elements from an array.
So let us see an example here. assume this is a array called as data and if you give just data of 0 the first element will be taken data of 1 will be this one and if I give data of 0 to 2 if I give 0 to 2 you have to observe here it will take the starting index as 0 and it will take only two elements so 0 and 1. The index 2 will not be considered. So it will be 0 then 1 and index 2 is not considered here. Whereas if you take data of 1 colon and you do not specify end here you do not specify end.
If you do not specify end value what happens it will take the length of the array. So it starts from index 1 which is index 1 value 2 is index 1 then till the end of the array. That means up to value 3. And if I have negative index, if I have negative index, negative index is the starting value.
The end value is not specified. So, what happens? If it is minus 2, this is considered as minus 1, this is considered as minus 2 because the last element will have index minus 1. Second last element will have index minus 2. So when I say minus 2 starting from this till the end of the array. Then if I just give the array name then everything will be included there and you can observe here this is index 0, this is index 1 and this is index 2 and if you use negative index this will be considered as minus 1 and this will be considered as minus 2 and this considered. as minus 3. So this is how indexing will be used and this is a simple example that shows how you can perform different slicing operations using the starting and ending index.
Please observe here I have an array of elements starting with value 1 and it ends with value 10 and I am going to have 1 is to 5 starting index 1 ending index So this is index 0, this is index 1 and index 1, 2, 3, 4 and you observe 5 is not considered. Then 1 colon ending index is not specified. So up to the end of the array. And here colon up to 5. So starting from 0, 0, 1. 2, 3, 4, 5 is not considered.
So, that is what you have to observe. Always in the end index, the end index value will not be considered. So, please observe each and everything negative indexing is also specified here. The last one is minus 3 to minus 1. So when I say minus 3, starting index is minus 3. So this is minus 1, this is minus 2 and this is minus 3. So when I give minus 3 starting index, index is minus 3. So, 8 is taken and minus 2 is also taken and minus 1 will not be considered. So, this is how the slicing operation can be performed on an index.
on an array indexing not index on array indexing. Please make a note of it. This is how we perform the slicing operations on an array with negative and positive indexing. And if you want to slice a two dimensional array, if you want to slice a two dimensional array, this is how we will be doing the slicing in the two dimensional array.
I have a two dimensional array here. The first row is having these five. values. Second row is having these 5 values and I am going to have a 2 dimensional array this way.
So this is the first index and the second index slicing has been given. Here the first in first dimension right so this is the first row now what happens here in the first row it is going to take the elements like this and here it is going to take 0 to 2 and there it is going to take the second element and here i am performing slicing in both the rows right so this is how it will be considered in the two-dimensional array let us see the output observe here I am going to have index 1. So that means it will be the second row. This is index 1, second row. In the second row it will take elements right from index 1. This is index 0 and this is index 1. 2, 3 and 4 will not be considered.
So 7, 8, 9 will be the output. And here what we are going to do? The 0 to 2. 0 to 2. So that means this is 0, this is 1 and this is 2 right.
So 0 to 2. So array 0 sorry row 0 will be considered, row 1 will also be considered and row 2 should also be considered but here we do not have index 2 okay because only two rows are there, row 0 and and row 1. In both the rows, I want to take the second element. So, in the first row, the index 2 is having value 3. In the second row, index 2 is having value 8. That is why we have 3 comma 8. And please observe here, you want to take 0 and 1 in the first row. 0, sorry, sorry, sorry.
You want to take elements from the 0th row as well as from the first row. There you have to consider values only from 1 to 4. So if this is the case it will consider the first row with 0 index. In the first row you want to take elements 1, 2, 3. So it will take index 1, index 2 and index 3. Index 4 will not be considered and then we will have 0, the second one is 1, right. So, index 1 will be considered which will be the second row and here again from index 1, 2 and 3 will be considered whereas 4 will not be considered.
So, you will have only 7, 8, 9. So, this is how slicing in two dimensional arrays can be done. And in this numpy module we have some interesting methods also. Now if you want to know what is the data type of an array object you can make use of the dtype property that returns the data type of the array and if you want to change the data type of an existing array you can make use of as type.
method as type method can be used it will create a copy of the array with that particular specified data type and this function will take a parameter and this parameter can be f or float. If you want to create an array with float value then you can use this parameter or if you want to create an array with integer values then you can create the int parameter. I will show you an example here.
right if you want to simply display the data type of an array you can use the d type property and if you want to change the data type you can observe here we are creating an array with float values we are creating an array with float values 5.1 6.3 7.21 these are the float values of the array i want to change the float values of this array to integer so what i do I use as type and I pass integer parameter here i or int right. You can use any of this i or int then a new array will be created which will have only integers of this particular value. And the third one I am creating an array with d type equal to f so that all these string values, these are all string values actually and these string values will be converted into float. So let us see the output of this. program.
This will be the output. See this is the first print statement where it tries to print the data type. So you have int 64 bits as the data type of this particular array. ARR1234 will have data type as int64. And the second one you are converting the float data type into integer data type and you create a new array.
And when we try to print the new array you can see only the integer values are taken 5, 6, 7 and the data type is integer which is of 32 bits. And here the list is actually a list of string values but I pass the data type as float so these string values are converted into float data type and it is creating a new array and you can observe it is creating 1 point 2 point 3 point 4 point from the string and the data type is float which is of 32 bits right so this is how d type property and as type methods can be used to find out the data type of an array or if you want to change the data type of an array you can use as type. Apart from that the array object has two different methods called as copy and view.
So these are another two interesting methods and the main difference between them is the copy method will Create a new array. So whatever changes you are going to do to the copy will not be affecting the original array. Whereas the view is just a view of the original array. It is something like an alias.
name or you can call it as a reference also right so what you do with your C++ and Java reference the same way this will also happen and whatever changes you are making to the view will affect the original array or whatever changes you make to the original array will also affect the view right so I will just show you an example you Simple example here you are importing the numpy as usual and I am creating an array with this particular list and I try to copy this array right. I am copying this array and I call it as b. And I am changing the value of this array. Value of this array's first index 0. I assign value 42. And if you try to print it here.
here when I make changes to a r it is not affecting the value of b. So b is having 1, 2, 3, 4, 5 because I copied this array correct and then after copying I change the first value to 42 for this particular array so you get this way good. But whereas when I make the copy with view function when I make changes to this particular array It is changing the value of the view also.
You can observe here. It is changing the value of view also. So this is the basic difference between copy and view.
Right. So whatever changes you are making to the copy will affect the original array when you create a new array with copy. But when you create a new array with view.
whatever changes you do with view will affect the original array or whatever changes you do with original array will affect the view also. Right, so this was the example we discussed. We will discuss the array shape and reshape in the next session. Thank you.