Difference between revisions of "Python-3.4.3/C2/Accessing-parts-of-arrays/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <ce...")
 
 
(One intermediate revision by one other user not shown)
Line 20: Line 20:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, we will learn to access and change:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, we will learn to access and change:
  
* Individual '''elements''' of single dimensional and''' '''multi-dimensional '''arrays'''.''' '''
+
* Individual '''elements''' of '''single dimensional''' and '''multi-dimensional '''arrays'''.
 
* '''Rows''' and '''columns''' of '''arrays'''.  
 
* '''Rows''' and '''columns''' of '''arrays'''.  
* Elements of an array, using '''slicing''' and '''striding'''.  
+
* '''Elements''' of an '''array''', using '''slicing''' and '''striding'''.  
 
+
 
+
  
 
|-
 
|-
Line 35: Line 33:
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 78: Line 74:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''import numpy as np'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''import numpy as np'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us create the two arrays in terminal.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us create the two '''arrays''' in '''terminal'''.
  
  
For this, we have to import '''numpy''' library.
+
For this, we have to import '''numpy library'''.
  
  
Type, '''import numpy as np''' and press '''Enter'''.
+
Type '''import numpy as np''' and press '''Enter'''.
  
  
Line 91: Line 87:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''A = np.arange(1,6)'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''A = np.arange(1,6)'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''A '''''is equal to''''' np '''''dot '''''arange '''''inside parentheses '''''1 '''''comma '''''6 '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''A '''''is equal to''''' np '''''dot '''''arange '''''inside parentheses '''''1 '''''comma '''''6 '''
  
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''C = np.arange(1,26).reshape(5,5)'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''C = np.arange(1,26).reshape(5,5)'''
  
  
Line 101: Line 97:
  
  
We have already learnt about '''arange()''' and''' reshape() methods''' in an earlier tutorial.
+
We have already learnt about '''arange''' and''' reshape methods''' in an earlier tutorial.
  
 
|-
 
|-
Line 107: Line 103:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now, let us see the contents of A and C.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now, let us see the contents of A and C.
  
Type, '''A '''
+
 
 +
Type '''A '''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''C '''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''C '''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''C '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''C '''
  
 
|-
 
|-
Line 128: Line 125:
  
 
<nowiki>[21, 22, 23, 24, 25]])</nowiki>
 
<nowiki>[21, 22, 23, 24, 25]])</nowiki>
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In '''A''', we have only one row with elements from 1 to 6.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In '''A''', we have only one row with '''elements''' from 1 to 6.
  
  
'''A''' is a '''one'''-'''dimensional''' '''array.'''
+
'''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.
+
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.'''
+
So '''C''' is a '''two dimensional array.'''
  
  
Line 153: Line 150:
  
  
To access, the element 3 in array '''A''', we say, A of 2.
+
To access, the '''element''' 3 in '''array A''', we say, A of 2.
  
  
Line 159: Line 156:
  
  
In Python, arrays are zero-indexed.  
+
In '''Python, arrays''' are '''zero-indexed'''.  
 
+
 
+
This means, the position of the element starts with 0 instead of 1.
+
 
+
  
  
 +
This means, the position of the '''element''' starts with 0 instead of 1.
  
 
|-
 
|-
Line 172: Line 166:
  
 
Type, '''<nowiki>C[2, 3]</nowiki>'''  
 
Type, '''<nowiki>C[2, 3]</nowiki>'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now, let us access the '''element''' 14 from array '''C'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now, let us access the '''element''' 14 from '''array C'''.  
  
  
Line 194: Line 188:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn how to change the value of an array.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn how to change the value of an '''array'''.
  
  
Line 200: Line 194:
  
  
For this we simply assign the new '''value''' after accessing the '''element'''.  
+
For this we simply assign the new value after accessing the '''element'''.  
  
  
Line 214: Line 208:
  
  
Type, '''A'''
+
Type '''A'''
  
 
|-
 
|-
Line 221: Line 215:
  
 
Highlight -3 and -14
 
Highlight -3 and -14
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''C'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''C'''
  
  
You can see that the elements are changed now.
+
You can see that the '''elements''' are changed now.
  
  
Likewise you can change any single element''' '''in an array.
+
Likewise you can change any single '''element''' in an '''array'''.
  
  
Line 246: Line 240:
  
  
Type, '''C '''''inside square brackets '''''2'''
+
Type '''C '''''inside square brackets '''''2'''
  
  
We can see that the third row of the array is displayed now.
+
We can see that the third row of the '''array''' is displayed now.
  
 
|-
 
|-
Line 258: Line 252:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Python programming supports negative indexing of arrays.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Python programming''' supports '''negative indexing''' of '''arrays'''.
  
  
Line 265: Line 259:
 
* -1 gives the last element and  
 
* -1 gives the last element and  
 
* -2 gives the second to last element of an array.
 
* -2 gives the second to last element of an array.
 
 
  
 
|-
 
|-
Line 280: Line 272:
  
 
Highlight the outputs
 
Highlight the outputs
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Or''' '''with negative indexing as''' C '''''inside square brackets minus '''''1'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Or with '''negative indexing''' as''' C '''''inside square brackets minus '''''1'''
  
  
Line 300: Line 292:
  
  
Notice that zeroes are displayed in the last row of the array C.
+
Notice that zeros are displayed in the last row of the '''array''' C.
  
 
|-
 
|-
Line 324: Line 316:
  
 
<nowiki>[start:stop]</nowiki>
 
<nowiki>[start:stop]</nowiki>
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us learn to slice an array.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us learn to '''slice''' an '''array'''.
  
  
Slicing of an array is done to access parts of an array.
+
'''Slicing''' of an '''array''' is done to access parts of an '''array'''.
  
  
Slicing syntax is inside square brackets '''start '''colon '''stop.'''
+
'''Slicing''' syntax is inside square brackets '''start colon stop.'''
  
 
|-
 
|-
Line 337: Line 329:
  
 
syntax '''<nowiki>[start:stop:step]</nowiki>'''
 
syntax '''<nowiki>[start:stop:step]</nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Striding uses the ‘step’ value to jump between the elements in an array
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Striding''' uses the ‘step’ value to jump between the '''elements''' in an '''array'''.
  
  
Striding syntax is inside square brackets '''start '''colon '''stop '''colon''' step.'''
+
'''Striding''' syntax is inside square brackets '''start '''colon '''stop colon step.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal.
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the terminal.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal'''.
  
 
|-
 
|-
Line 354: Line 346:
  
  
0 and 3 corresponds to start and stop values for row slicing and 2 corresponds to column index.
+
0 and 3 corresponds to start and stop values for row '''slicing''' and 2 corresponds to column index.
  
  
Line 360: Line 352:
  
  
Hence we have sliced the array.
+
Hence we have '''sliced''' the '''array'''.
  
 
|-
 
|-
Line 379: Line 371:
  
  
2 corresponds to row index and 0 and 3 corresponds to start and stop values for column slicing.
+
2 corresponds to row index and 0 and 3 corresponds to start and stop values for column '''slicing'''.
  
 
|-
 
|-
Line 392: Line 384:
  
  
Obtain the following elements one by one from array C.
+
Obtain the following '''elements''' one by one from '''array''' C.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.  
  
 
|-
 
|-
Line 406: Line 398:
  
  
We get the elements 7 and 8.
+
We get the '''elements''' 7 and 8.
  
 
|-
 
|-
Line 416: Line 408:
  
  
We get the elements 1, 6, 11 and 16
+
We get the '''elements''' 1, 6, 11 and 16
  
 
|-
 
|-
Line 426: Line 418:
  
  
We get the elements 6, 11, 16 and 2
+
We get the '''elements''' 6, 11, 16 and 2
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>C[1:, 0]</nowiki>'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>C[1:, 0]</nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also get the same elements by typing '''C '''''square brackets '''''1 '''colon comma''' 0'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also get the same elements by typing '''C '''''inside square brackets '''''1 '''colon comma''' 0'''
  
 
|-
 
|-
Line 443: Line 435:
  
  
<nowiki>Obtain the elements [[8, 9], [13, -14]] from array C. </nowiki>
+
Obtain the '''elements''' [[8, 9], [13, -14]] from '''array''' C.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.  
  
 
|-
 
|-
Line 496: Line 488:
  
 
Highlight the output
 
Highlight the output
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The step 2 specifies the jump between the elements.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''step''' 2 specifies the '''jump''' between the '''elements'''.  
  
  
Line 502: Line 494:
  
  
If no step is specified, a default value of 1 is assumed.  
+
If no '''step''' is specified, a default value of 1 is assumed.  
  
  
Line 508: Line 500:
  
  
We get the elements, as shown.
+
We get the '''elements''' as shown.
  
 
|-
 
|-
Line 521: Line 513:
  
  
Obtain the following elements from array C.
+
Obtain the following '''elements''' from '''array''' C.
  
 
|-
 
|-
Line 544: Line 536:
 
# Access and change '''rows''' and '''columns''' of arrays by specifying the '''row''' and '''column''' numbers.  
 
# Access and change '''rows''' and '''columns''' of arrays by specifying the '''row''' and '''column''' numbers.  
 
# '''Slice''' and '''stride''' on '''arrays'''.  
 
# '''Slice''' and '''stride''' on '''arrays'''.  
 
 
  
 
|-
 
|-
Line 554: Line 544:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| 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 <nowiki>[[21, 22], [31, 32]] </nowiki>from the given array B
+
  
  
 +
# How do we access the '''element''' 18 from the given array A?
 +
# How do we obtain the '''elements''' <nowiki>[[21, 22], [31, 32]] </nowiki>from the given '''array''' B?
  
 
|-
 
|-
Line 570: Line 558:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,
  
# The element 18 in array A has index number 2. Hence, we can access it as A of 2
+
# 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, <nowiki>B[1:3, 1:3]</nowiki>
+
# To obtain the central four numbers in the '''array''' B, we say, B ''inside square brackets ''1 colon 3 comma 1 colon 3
  
 
|-
 
|-

Latest revision as of 22:00, 6 May 2018

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 zeros 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 inside 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,


  1. Manipulate single and multi dimensional arrays.
  2. Access and change individual elements by using their index numbers.
  3. Access and change rows and columns of arrays by specifying the row and column numbers.
  4. Slice and stride on arrays.
Show Slide

Evaluation


Here are some self assessment questions for you to solve.


  1. How do we access the element 18 from the given array A?
  2. How do we obtain the elements [[21, 22], [31, 32]] from the given array B?
Show Slide


Solutions


And the answers,
  1. The element 18 in array A has index number 2. Hence, we can access it as A of 2
  1. To obtain the central four numbers in the array B, we say, B inside square brackets 1 colon 3 comma 1 colon 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.

Contributors and Content Editors

Nancyvarkey, Priyacst