Visual Cue
|
Narration
|
Show Slide
|
Welcome to the spoken tutorial on "Accessing parts of arrays".
|
Show Slide
Objectives
|
In this tutorial, we will learn to access and change:
- Individual elements of single dimensional and multi-dimensional arrays.
- Rows and columns of arrays.
- Elements of an array, using slicing and striding.
|
Show Slide
System Specifications
|
To record this tutorial, I am using
- Ubuntu Linux 16.04 operating system
- Python 3.4.3 and
- IPython 5.1.0
|
Show Slide
Pre-requisite slide
|
To practise this tutorial, you should know how to
- run basic Python commands on the IPython console.
- use arrays.
If not, see the relevant Python tutorials on this website.
|
Show Slide
Sample Arrays
|
Let us begin with the help of an example.
Consider two arrays, A and C.
We will use these arrays throughout this tutorial.
|
[Terminal]
Open the terminal, type ipython3 and press Enter
|
Let us start ipython.
Open the terminal.
Type ipython3 and press Enter.
|
Type import numpy as np
|
Let us create the two arrays in terminal.
For this, we have to import numpy library.
Type, import numpy as np and press Enter.
From here onwards, please remember to press the Enter key after typing every command on the terminal.
|
Type, A = np.arange(1,6)
|
Type, A is equal to np dot arange inside parentheses 1 comma 6
|
Type, C = np.arange(1,26).reshape(5,5)
Highlight arange, reshape
|
Type, C is equal to np dot arange inside parentheses 1 comma 26 dot reshape inside parentheses 5 comma 5
We have already learnt about arange() and reshape() methods in an earlier tutorial.
|
Type A
|
Now, let us see the contents of A and C.
Type, A
|
Type, C
|
Type, C
|
Highlight
A = array([1, 2, 3, 4, 5])
C = array([[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
|
In A, we have only one row with elements from 1 to 6.
A is a one-dimensional array.
In C, we have 1 to 26 elements in the form of matrix of 5 rows and 5 columns.
So C is a two-dimensional array.
<<PAUSE>>
|
Highlight 3 in A = array([1, 2, 3, 4, 5])
Type A[2]
|
Next, let us see about accessing individual elements in an array.
To access, the element 3 in array A, we say, A of 2.
Type A inside square brackets 2
In Python, arrays are zero-indexed.
This means, the position of the element starts with 0 instead of 1.
|
Highlight array C in slide
Type, C[2, 3]
|
Now, let us access the element 14 from array C.
14 is in the third row and the fourth column.
To do this, we say, C of 2,3.
Type C inside square brackets 2 comma 3.
<<PAUSE>>
|
Highlight array A, C in slide
Type A[2] = -3
|
Next we will learn how to change the value of an array.
We shall now change 3 to -3 in A and 14 to -14 in C.
For this we simply assign the new value after accessing the element.
Type, A inside square brackets 2 is equal to minus 3
|
Type, C[2, 3] = -14
|
Type, C inside square brackets 2 comma 3 is equal to minus 14
|
Type, A
|
Let us check our operations.
Type, A
|
Type C
Highlight -3 and -14
|
Type, C
You can see that the elements are changed now.
Likewise you can change any single element in an array.
<<PAUSE>>
|
Highlight array C in slide
Type, C[2]
|
Next let us learn to change more than one elements at a time.
First with rows and then with columns.
Let us access one row of C, say the third row.
Type, C inside square brackets 2
We can see that the third row of the array is displayed now.
|
Slide:
Negative Indexing
|
Python programming supports negative indexing of arrays.
This means the index value of
- -1 gives the last element and
- -2 gives the second to last element of an array.
|
Type C[4]
|
We can access the last row of C in 2 ways.
Type C inside square brackets 4
|
Type C[-1]
Highlight the outputs
|
Or with negative indexing as C inside square brackets minus 1
Notice that both the outputs are same.
|
Type C[-1] = [0, 0, 0, 0, 0]
|
Now, we will learn to change the last row into all zeros.
Type C inside square brackets minus 1 is equal to inside square brackets 0 comma 0 comma 0 comma 0 comma 0
|
Type C
|
Type C
Notice that zeroes are displayed in the last row of the array C.
|
Type C[-1] = 2
|
We can also type, C inside square brackets minus 1 is equal to 2.
|
Type C
Highlight the last row
|
Type C
And check with the changes made.
<<PAUSE>>
|
Slicing slide
[start:stop]
|
Now let us learn to slice an array.
Slicing of an array is done to access parts of an array.
Slicing syntax is inside square brackets start colon stop.
|
Striding slide
syntax [start:stop:step]
|
Striding uses the ‘step’ value to jump between the elements in an array
Striding syntax is inside square brackets start colon stop colon step.
|
Switch to terminal.
|
Switch back to the terminal.
|
Type C[0:3,2]
Highlight array([ 3, 8, 13])
|
Type, C inside square brackets 0 colon 3 comma 2
0 and 3 corresponds to start and stop values for row slicing and 2 corresponds to column index.
We get the elements of rows indexed from 0 to 2 and column indexed by 2.
Hence we have sliced the array.
|
Highlight in C ([11, 12, 13])
Type C[2,0:3]
Highlight 2 and 0:3
|
Now we will access the elements of row with index 2, and first 2 columns.
Type C inside square brackets 2 comma 0 colon 3
2 corresponds to row index and 0 and 3 corresponds to start and stop values for column slicing.
|
Show Slide
Exercise 1
|
Pause the video.
Try this exercise and then resume the video.
Obtain the following elements one by one from array C.
|
Switch to terminal
|
Switch to the terminal for the solution.
|
Type C[1, 1:3]
Highlight[7,8]
|
Type C inside square brackets 1 comma 1 colon 3
We get the elements 7 and 8.
|
Type C[0:4, 0]
Highlight [1, 6, 11, 16]
|
Type C inside square brackets 0 colon 4 comma 0
We get the elements 1, 6, 11 and 16
|
Type C[1:5, 0]
Highlight [6, 11, 16, 0]
|
Type C inside square brackets 1 colon 5 comma 0
We get the elements 6, 11, 16 and 2
|
Type C[1:, 0]
|
We can also get the same elements by typing C square brackets 1 colon comma 0
|
Show Slide
Exercise 2
|
Pause the video.
Try this exercise and then resume the video.
Obtain the elements [[8, 9], [13, -14]] from array C.
|
Switch to terminal
|
Switch to the terminal for the solution.
|
Type C[1:3, 2:4]
Highlight the output
|
Type, C inside square brackets 1 colon 3 comma 2 colon 4
We got the required elements.
<<PAUSE>>
|
Type C[0:5:2, 0:5:2]
|
Next we will learn the idea of striding using the smaller array C.
We will try to access only the odd rows and columns i.e first, third and fifth.
Type, C inside square brackets 0 colon 5 colon 2 comma 0 colon 5 colon 2
|
Type C[::2, ::2]
Highlight the output
|
We can also type C inside square brackets colon colon 2 comma colon colon 2
We can see that only the odd rows and columns are displayed.
|
Highlight 2 in C[::2, ::2]
Type C[1::2, ::2]
Highlight the output
|
The step 2 specifies the jump between the elements.
This is called striding.
If no step is specified, a default value of 1 is assumed.
Type C inside square brackets 1 colon colon 2 comma colon colon 2
We get the elements, as shown.
|
Show Slide
Exercise 3
|
Pause the video.
Try this exercise and then resume the video.
Obtain the following elements from array C.
|
Show Slide Solution 3
|
The solution is on your screen.
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial.
In this tutorial, we have learnt to,
- Manipulate single and multi dimensional arrays.
- Access and change individual elements by using their index numbers.
- Access and change rows and columns of arrays by specifying the row and column numbers.
- Slice and stride on arrays.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve
- How do we access the element 18 from the given array A?
- How do we obtain the elements [[21, 22], [31, 32]] from the given array B
|
Show Slide
Solutions
|
And the answers,
- The element 18 in array A has index number 2. Hence, we can access it as A of 2
- To obtain the central four numbers in the array B, we say, B[1:3, 1:3]
|
Show Slide Forum
|
Please post your timed queries in this forum.
|
Show Slide Fossee Forum
|
Please post your general queries on Python in this forum.
|
Slide Textbook Companion
|
FOSSEE team coordinates the TBC project.
|
Show Slide Acknowledgement
|
Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website.
|
Show Slide
Thank You
|
This is Priya from IIT Bombay signing off. Thanks for watching.
|