Python-3.4.3/C3/Getting-started-with-arrays/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Getting started with arrays.
00:06 In this tutorial, you will learn to,

Create arrays using data

Create arrays from lists

Perform basic array operations

Create an identity matrix

and Use the method zeros

00:24 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3 and

IPython 5.1.0

00:39 To practice this tutorial, you should know how to use Lists.If not, see the relevant Python tutorials on this website.
00:50 Arrays are homogeneous data structures. All elements in it must be of same data type.
00:58 In this tutorial, we will be using numpy library which we used in earlier tutorial.
01:05 Let us first open the terminal by pressing Ctrl+Alt+T keys simultaneously.
01:12 Let us start ipython by typing ipython3 and press Enter. We can see the ipython prompt.
01:22 Now we will import numpy. Type, import numpy as np and press Enter.
01:32 Now let us see how to create arrays.
01:36 From here onwards, please remember to press the Enter key after typing every command on the terminal.
01:44 Type, a1 is equal to np dot array inside parentheses inside square brackets 1 comma 2 comma 3 comma 4
01:54 Type a1 Notice that we have created a one dimensional array here.
02:02 Also notice that the object we passed to create an array is a list, i.e a1.
02:09 Next we will see how to create two dimensional array.
02:14 Two dimensional array is created by converting a list of lists to an array.
02:20 Type, a2 is equal to np dot array inside parentheses inside square brackets again inside square brackets 1 comma 2 comma 3 comma 4 comma inside square brackets 5 comma 6 comma 7 comma 8
02:38 Type a2 This is our 2-dimensional array.
02:44 Next we will see about arange method.
02:48 To arrange the elements in an array we use arange method. The syntax is shown here.
02:57 Type ar is equal to np dot arange inside parentheses 1 comma 9.
03:04 Type print inside parentheses ar
03:08 Here, 1 is the start value and 9 is the stop value.
03:13 As you can see, we obtained a one dimensional array between 1 and 9 with 1 included and 9 excluded.
03:22 It will give the elements one less than the stop value.
03:26 Can we make a two dimensional array of order 2 by 4? Yes, we can do it.
03:33 We will use reshape method to change the shape of an array.
03:38 The syntax is: object.reshape inside parentheses rows comma columns
03:45 Switch back to the terminal.
03:48 Type, ar dot reshape inside parentheses 2 comma 4.
03:54 Type ar Shape of the original array ar is not changed.
04:00 If you want to change the shape of the original array, type ar dot shape is equal to inside parentheses 2 comma 4.
04:11 Type ar We can see that the shape of the original array ar is changed now.
04:20 To find the shape of an array we can use the method shape.
04:25 It returns a tuple of the shape of an array.

A tuple is nothing but an ordered list of elements.

04:34 Let us check the shape of the arrays we have created so far.
04:38 Type a2 dot shape a2.shape object is a tuple, and it returned a tuple (2, 4).
04:47 Pause the video here, try out the following exercise and resume the video.
04:52 Find out the shape of the arrays a1 and ar which we have created earlier in this tutorial.
04:58 Let us see the solution. Type, a1 dot shape
05:04 Since a1 is a single dimensional array, the column is empty.
05:09 Type, ar dot shape ar is a two dimensional array.
05:15 Now let us try to create a new array with elements of different datatypes.
05:21 Type a3 is equal to np dot array inside parentheses inside square brackets 1 comma 2 comma 3 comma inside single quotes a string
05:33 Arrays handle elements with the same datatype.
05:37 Here we are handling with different datatypes. So it should give us error.
05:44 Type a3 But we did not get any error. Because all the elements get implicitly converted as strings.
05:54 This is how array works.
05:57 Note that the output mentions dtype=’<U21'
06:04 A dtype is the datatype required to hold the objects in sequence.
06:10 The characters of dtype i.e. ’<U21' might differ with python version.
06:17 Next we will see about identity matrix.
06:21 It is a square matrix of order (n,n) with ones on the main diagonal and all other elements as zeros.
06:29 The syntax is identity inside parentheses n.
06:34 Let us see how to create a 2 by 2 identity matrix.
06:39 Type, np dot identity inside parentheses 2.
06:45 We can see all ones in the main diagonal as expected.
06:50 Next is Zeros method.
06:53 It creates an m by n matrix with all elements as 0.
06:58 The Syntax is: zeros inside parentheses inside parentheses m, n
07:05 Let us create an array of the shape (4, 5) with all the elements zero.
07:11 Type, np dot zeros inside parentheses inside parentheses 4 comma 5.
07:18 The default output of identity and zeros method are in float datatype.
07:24 Explore the below functions on your own:

zeros_like

ones

ones_like

07:34 Try the following.
07:36 First check the value of a1 which we assigned earlier.
07:41 Type a1 We see that a1 is a single dimensional array.
07:48 Let us now try a1 multiplied by 2 It returned a new array with all the elements multiplied by 2.
07:58 Now let us again check the contents of a1. Note that the value of a1 still remains the same.
08:06 Similarly we will try with addition.
08:10 Type, a1 plus 2 It returns a new array, with all the elements summed with two.
08:18 Type a1 . But again notice that the value of a1 has not been changed.
08:26 Let us try with a1 plus equal to 2
08:31 Type, a1 This will change the array a1 itself as we are assigning the new output to a1.
08:41 We can use all the mathematical operations with arrays. Next, we will see how to add two arrays.
08:50 Type, a1 is equal to np dot array inside parentheses inside square brackets 1, 2, 3, 4
09:00 Type a2 is equal to np dot array inside parentheses inside square brackets 5, 6, 7, 8
09:10 Type a1 plus a2This returns an array by adding element by element.
09:18 Type a1 multiplied by a2 It returns an array with element by element multiplication.
09:27 This brings us to the end of the end of this tutorial.
09:31 In this tutorial, we have learnt to,

1. Create an array using the array() function.

2. Perform some basic operations on arrays like addition and multiplication.

3. Use methods like -

shape , arange, reshape, identity andzeros

09:50 Here is a self assessment question for you to solve
09:54 x is equal to np.array inside parentheses inside square brackets 1, 2, 3, inside square brackets 5, 6, 7 is a valid statement?

True False

10:10 And the answer is False.
10:13 The correct way is to assign the elements as a list of lists and then convert it to an array.
10:19 That is x is equal to np.array inside parentheses inside square brackets again inside square brackets 1, 2, 3, inside square brackets 5, 6, 7
10:35 Please post your timed queries in this forum.
10:39 Please post your general queries on Python in this forum.
10:44 FOSSEE team coordinates the TBC project.
10:48 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.For more details, visit this website.
10:57 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya, Pratik kamble