Python-3.4.3/C2/Getting-started-with-arrays/English
| |
|
| Show Slide | Welcome to the spoken tutorial on Getting started with arrays. |
| Show Slide
Learning objectives
|
In this tutorial, you will learn to,
|
| Show Slide
System Requirements |
To record this tutorial, I am using
|
| Show Slide
Pre-requisite slide |
To practice this tutorial, you should know how to use Lists.
|
| Show Slide
Overview of array |
Arrays are homogeneous data structures.
|
| open the Terminal by pressing Ctrl+Alt+T keys simultaneously | In this tutorial, we will be using numpy library which we used in earlier tutorial.
|
| type python3
Highlight ipython prompt |
Let us start ipython by typing ipython3 and press Enter.
|
| Type, import numpy as np | Now we will import numpy.
|
| Type, a1 = np.array([1,2,3,4])
|
Now let us see how to create arrays.
|
| Type a1
|
Type, a1
|
| Type
a2 = np.array([[1,2,3,4],[5,6,7,8]])
|
Next we will see how to create two dimensional array.
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 |
| Type a2
|
Type a2
<<PAUSE>> |
| slide : arange method
|
Next we will see about arange method.
arange([start,] stop, [step,] dtype=None) |
| Type, ar = np.arange(1,9) | Type, ar is equal to np dot arange inside parentheses 1 comma 9. |
| type print (ar)
|
Type, print inside parentheses ar
<<PAUSE>> |
| Can we make a two dimensional array of order 2 by 4?
| |
| slide reshape method
|
We will use reshape method to change the shape of an array.
object.reshape inside parentheses rows comma columns |
| Switch to terminal | Switch back to the terminal. |
| Type, br = ar.reshape(2,4) | Type, ar dot reshape inside parentheses 2 comma 4. |
| type ar | type ar
|
| Type, ar.shape = (2,4) | If you want to change the shape of the original array, type
ar dot shape is equal to inside parentheses 2 comma 4. |
| Type ar | Type ar
|
| Show Slide
shape of an array
|
To find the shape of an array we can use the method shape.
|
| Type
a2.shape
|
Let us check the shape of the arrays we have created so far.
|
| Show Slide
Assignment 1
|
Pause the video here, try out the following exercise and resume the video.
|
| Type a1.shape | Let us see the solution.
|
| Type, ar.shape | Type, ar dot shape
|
| Type, a3 = np.array([1,2,3,'a string']) | Now let us try to create a new array with elements of different datatypes.
|
| Type a3
|
Arrays handle elements with the same datatype.
|
| Highlight dtype='<U21')
|
This is how array works.
|
| Show Slide
identity(n) method
|
Next we will see about identity matrix.
|
| type np.identity(2) | Let us see how to create a 2 by 2 identity matrix.
|
| Show Slide
zeros(shape) method
|
Next is Zeros method.
|
| type
np.zeros((4,5)) |
Let us create an array of the shape (4, 5) with all the elements zero.
|
| But box on the arrays iden: | The default output of identity and zeros method are in float datatype. |
| Show Slide
Learning exercise
|
Explore the below functions on your own:
|
| type, a1 | Try the following.
|
| type
a1 * 2 |
Let us now try a1 multiplied by 2
|
| Type a1 | Now let us again check the contents of a1.
|
| Type a1 + 2 | Similarly we will try with addition.
|
| Type a1 | Type a1
|
| Type a1 += 2 | Let us try with a1 plus equal to 2 |
| Type a1 | Type, a1
|
| Type a1 = np.array([1, 2, 3, 4]) | We can use all the mathematical operations with arrays.
|
| Type, a2 = np.array([5, 6, 7, 8]) | Type, a2 is equal to np dot array inside parentheses inside square brackets 5, 6, 7, 8 |
| Type, a1 + a2 | Type, a1 plus a2
|
| Type, a1 * a2
|
Type, a1 multiplied by a2
|
| Show Slide
Summary slide
|
This brings us to the end of the end of this tutorial.
shapearange reshapeidentityzeros |
| Show Slide
Self assessment questions slide
|
Here is a self assessment question for you to solve
x = np.array([1, 2, 3], [5, 6, 7]) is a valid statement ?
|
| Show Slide
Solution of self assessment questions on slide
|
And the answer is False.
|
| 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
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
|
| Previous slide | This is Priya from IIT Bombay signing off. Thanks for joining. |