Python-3.4.3/C3/Accessing-parts-of-arrays/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on "Accessing parts of arrays".
00:07 In this tutorial, we will learn to access and change: Individual elements of single dimensional and multi-dimensional arrays.
00:17 Rows and columns of arrays.
00:20 Elements of an array, using slicing and striding.
00:25 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

00:33 Python 3.4.3 and IPython 5.1.0
00:40 To practise this tutorial, you should know how to run basic Python commands on the IPython console.
00:49 use arrays.
00:51 If not, see the relevant Python tutorials on this website.
00:57 Let us begin with the help of an example.
01:01 Consider two arrays, A and C.

We will use these arrays throughout this tutorial.

01:09 Let us start ipython.

Open the terminal.

01:15 Type ipython3 and press Enter.
01:21 Let us create the two arrays in terminal.

For this, we have to import numpy library.

01:29 Type import numpy as np and press Enter.
01:36 From here onwards, please remember to press the Enter key after typing every command on the terminal.
01:43 Type A is equal to np dot arange inside parentheses 1 comma 6
01:51 Type, C is equal to np dot arange inside parentheses 1 comma 26 dot reshape inside parentheses 5 comma 5
02:03 We have already learnt about arange and reshape methods in an earlier tutorial.
02:09 Now, let us see the contents of A and C.

Type A

02:16 Type C
02:19 In A, we have only one row with elements from 1 to 6.

A is a one dimensional array.

02:28 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.

02:40 Next, let us see about accessing individual elements in an array.
02:45 To access, the element 3 in array A, we say, A of 2.
02:51 Type A inside square brackets 2
02:56 In Python, arrays are zero-indexed.

This means, the position of the element starts with 0 instead of 1.

03:06 Now, let us access the element 14 from array C.
03:11 14 is in the third row and the fourth column.

To do this, we say, C of 2,3.

03:19 Type C inside square brackets 2 comma 3.
03:24 Next we will learn how to change the value of an array.
03:29 We shall now change 3 to -3 in A and 14 to -14 in C.
03:37 For this we simply assign the new value after accessing the element.
03:43 Type, A inside square brackets 2 is equal to minus 3
03:50 Type, C inside square brackets 2 comma 3 is equal to minus 14
03:57 Let us check our operations.

Type A

04:03 Type C

You can see that the elements are changed now.

04:10 Likewise you can change any single element in an array.
04:15 Next let us learn to change more than one elements at a time.

First with rows and then with columns.

04:24 Let us access one row of C, say the third row.
04:29 Type C inside square brackets 2
04:34 We can see that the third row of the array is displayed now.
04:40 Python programming supports negative indexing of arrays.
04:45 This means the index value of -1 gives the last element and -2 gives the second to last element of an array.
04:56 We can access the last row of C in 2 ways.
05:01 Type C inside square brackets 4
05:06 Or with negative indexing as C inside square brackets minus 1

Notice that both the outputs are same.

05:17 Now, we will learn to change the last row into all zeros.
05:22 Type C inside square brackets minus 1 is equal to inside square brackets 0 comma 0 comma 0 comma 0 comma 0
05:34 Type C

Notice that zeros are displayed in the last row of the array C.

05:42 We can also type, C inside square brackets minus 1 is equal to 2.
05:50 Type C

And check with the changes made.

05:57 Now let us learn to slice an array.
06:00 Slicing of an array is done to access parts of an array.
06:05 Slicing syntax is inside square brackets start colon stop.
06:11 Striding uses the ‘step’ value to jump between the elements in an array.
06:17 Striding syntax is inside square brackets start colon stop colon step.
06:25 Switch back to the terminal.
06:28 Type, C inside square brackets 0 colon 3 comma 2
06:34

0 and 3 corresponds to start and stop values for row slicing and 2 corresponds to column index.

06:44 We get the elements of rows indexed from 0 to 2 and column indexed by 2.

Hence we have sliced the array.

06:54 Now we will access the elements of row with index 2, and first 2 columns.
07:01 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.

07:17 Pause the video. Try this exercise and then resume the video.
07:23 Obtain the following elements one by one from array C.
07:28 Switch to the terminal for the solution.
07:32 Type C inside square brackets 1 comma 1 colon 3
07:39 We get the elements 7 and 8.
07:43 Type C inside square brackets 0 colon 4 comma 0
07:50 We get the elements 1, 6, 11 and 16
07:55 Type C inside square brackets 1 colon 5 comma 0
08:02 We get the elements 6, 11, 16 and 2
08:07 We can also get the same elements by typing C inside square brackets 1 colon comma 0
08:16 Pause the video. Try this exercise and then resume the video.
08:22 Obtain the elements [[8, 9], [13, -14]] from array C.
08:28 Switch to the terminal for the solution.
08:31 Type, C inside square brackets 1 colon 3 comma 2 colon 4
08:39 We got the required elements.
08:42 Next we will learn the idea of striding using the smaller array C.
08:47 We will try to access only the odd rows and columns i.e first, third and fifth.
08:54 Type, C inside square brackets 0 colon 5 colon 2 comma 0 colon 5 colon 2
09:04 We can also type C inside square brackets colon colon 2 comma colon colon 2
09:13 We can see that only the odd rows and columns are displayed.
09:18 The step 2 specifies the jump between the elements.

This is called striding.

09:26 If no step is specified, a default value of 1 is assumed.
09:32 Type C inside square brackets 1 colon colon 2 comma colon colon 2
09:41 We get the elements as shown.
09:44 Pause the video. Try this exercise and then resume the video.
09:50 Obtain the following elements from array C.
09:54 The solution is on your screen.
09:57 This brings us to the end of this tutorial.

In this tutorial, we have learnt to, Manipulate single and multi dimensional arrays.

10:08 Access and change individual elements by using their index numbers.
10:14 Access and change rows and columns of arrays by specifying the row and column numbers.
10:21 Slice and stride on arrays.
10:24 Here are some self assessment questions for you to solve.
10:28 How do we access the element 18 from the given array A?
10:33 How do we obtain the elements [[21, 22], [31, 32]] from the given array B?
10:40 And the answers,

First. The element 18 in array A has index number 2. Hence, we can access it as A of 2

10:50 Second. To obtain the central four numbers in the array B, we say, B inside square brackets 1 colon 3 comma 1 colon 3
11:01 Please post your timed queries in this forum.
11:05 Please post your general queries on Python in this forum.
11:10 FOSSEE team coordinates the TBC project.
11:14 Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

11:24 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya