Difference between revisions of "Python-3.4.3/C3/Manipulating-lists/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 15: Line 15:
  
 
* '''slicing''' and '''striding''' of '''lists'''  
 
* '''slicing''' and '''striding''' of '''lists'''  
* '''Sort''' and '''reverse''' '''lists'''.  
+
* '''Sort''' and '''reverse lists'''.  
 
+
 
+
  
 
|-
 
|-
Line 28: Line 26:
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 60: Line 56:
  
  
The element with the stop '''index''' value will not be included.
+
The '''element''' with the '''stop index''' value will not be included.
  
 
|-
 
|-
Line 83: Line 79:
  
 
'''<nowiki>primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] </nowiki>'''
 
'''<nowiki>primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] </nowiki>'''
| 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 understand the slicing with an example.
+
| 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 understand the '''slicing''' with an example.
  
  
Line 101: Line 97:
  
  
Observe that the first element we want is 11 which has index 4 in the list.
+
Observe that the first '''element''' we want is 11 which has index 4 in the list.
  
  
Line 107: Line 103:
  
  
Also, 19 is the last element we require which has index 7 in the list.
+
Also, 19 is the last '''element''' we require which has '''index''' 7 in the '''list'''.
  
  
The element with '''index''' equal to '''stop''' value will not be included.
+
The '''element''' with '''index''' equal to '''stop value''' will not be included.
  
  
Line 140: Line 136:
  
  
7 is the last element we require which has index 3 in the list.
+
7 is the last '''element''' we require which has '''index''' 3 in the '''list'''.
  
  
Line 150: Line 146:
 
|-
 
|-
 
| 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>num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] </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>num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] </nowiki>'''
| 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 to use step '''value''' in slicing.
+
| 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 to use '''step value''' in slicing.
  
  
Type 0 to 13 in a list and assign it to a variable '''num''' as shown.''' '''
+
Type 0 to 13 in a '''list''' and assign it to a variable '''num''' as shown.
  
 
|-
 
|-
Line 165: Line 161:
  
  
We will specify the '''step '''value in which the '''slice''' must be obtained.  
+
We will specify the '''step value''' in which the '''slice''' must be obtained.  
  
  
Line 174: Line 170:
  
  
It is called '''striding''' of list.
+
It is called '''striding''' of '''list'''.
  
  
Line 219: Line 215:
  
  
'''Start''' or '''end''' value can be negative to indicate that they are counted from the end of the list.
+
'''Start''' or '''end''' value can be negative to indicate that they are counted from the end of the '''list'''.
  
 
|-
 
|-
Line 259: Line 255:
  
  
It gives us every third element from the list (i.e) multiples of 3.
+
It gives us every third '''element''' from the '''list''' (i.e) multiples of 3.
  
  
Line 266: Line 262:
 
|-
 
|-
 
| 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>a = [5, 1, 6, 7, 7, 10] </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>a = [5, 1, 6, 7, 7, 10] </nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us learn to sort a list.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us learn to sort a '''list'''.
  
  
Type, '''a '''''is equal to''''' '''''inside square brackets '''''5, 1, 6, 7, 7, 10 '''
+
Type, '''a '''''is equal to inside square brackets '''''5, 1, 6, 7, 7, 10 '''
  
 
|-
 
|-
 
| 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.sort()'''
 
| 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.sort()'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''sort method''' is used to sort a list.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''sort method''' is used to sort a '''list'''.
  
  
Line 285: Line 281:
  
  
We can see that the contents of the '''list''' '''a''' is sorted now.
+
We can see that the contents of the '''list a''' is sorted now.
  
 
|-
 
|-
Line 291: Line 287:
  
 
'''Sorted()'''
 
'''Sorted()'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| * Python provides a built-in '''function''' called '''sorted.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"|
* '''sorted''' '''function''' sorts the '''list''' which is passed as an '''argument''' to it.
+
* Python provides a built-in '''function''' called '''sorted.'''
 +
* '''sorted function''' sorts the '''list''' which is passed as an '''argument''' to it.
 
* It returns a new '''sorted list. '''
 
* It returns a new '''sorted list. '''
 
 
  
 
|-
 
|-
Line 307: Line 302:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa = sorted(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 '''sa = sorted(a) '''
| 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 store this '''sorted''' '''list''' into another '''list variable sa.'''
+
| 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 store this '''sorted list''' into another '''list variable sa.'''
  
  
Line 314: Line 309:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the sorted list, type '''sa'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''sorted list''', type '''sa'''
  
 
|-
 
|-
Line 332: Line 327:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''r'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''r'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the list '''r''', type, '''r'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''list r''', type, '''r'''
  
  
Line 338: Line 333:
  
  
The original '''list''' '''r''' is changed now.
+
The original '''list r''' is changed now.
  
 
|-
 
|-
 
| 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>r = [1, 2, 3, 4, 5] </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>r = [1, 2, 3, 4, 5] </nowiki>'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To '''reverse''' a list, we can also use '''striding''' with negative '''values'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To '''reverse''' a '''list''', we can also use '''striding''' with negative '''values'''.  
  
  
Line 353: Line 348:
 
|-
 
|-
 
| 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>ra = r[::-1] </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>ra = r[::-1] </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 store this new '''reversed''' '''list''' in another''' variable'''.  
+
| 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 store this new '''reversed list''' in another''' variable'''.  
  
  
Line 360: Line 355:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''ra'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''ra'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the reversed list, type '''ra'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''reversed list''', type '''ra'''
  
 
|-
 
|-
Line 376: Line 371:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Given below is the list of marks of a student in an examination.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Given below is the '''list''' of marks of a student in an examination.
  
  
Obtain a list with marks in descending order.  
+
Obtain a '''list''' with marks in descending order.  
  
 
|-
 
|-
 
| 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
 
| 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
| 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 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 back 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;"| Type '''<nowiki>marks = [99, 67, 47, 100, 50, 75, 62] </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>marks = [99, 67, 47, 100, 50, 75, 62] </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 the list of marks as shown.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type the '''list''' of marks as shown.
  
 
|-
 
|-
Line 425: Line 420:
 
* Obtain the parts of '''lists''' using '''slicing''' and '''striding'''.  
 
* Obtain the parts of '''lists''' using '''slicing''' and '''striding'''.  
 
* '''Sort lists''' using the '''sort''' method.  
 
* '''Sort lists''' using the '''sort''' method.  
* Use the method '''reverse''' to '''reverse''' the '''lists'''.  
+
* Use the method '''reverse''' to reverse the '''lists'''.  
 
+
 
+
  
 
|-
 
|-
Line 439: Line 432:
  
  
# Given the list '''primes'''. How do you obtain the last 4 prime numbers?  
+
# Given the '''list primes'''. How do you obtain the last 4 prime numbers?  
# Given a list, p, of unknown length. Obtain the first 3 characters of it.  
+
# Given a '''list p''', of unknown length. Obtain the first 3 characters of it.  
 
+
 
+
  
 
|-
 
|-
Line 453: Line 444:
 
| 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 last four primes can be obtained from the given list as,  
+
# The last four primes can be obtained from the given '''list''' as,  
  
 
'''primes''' ''inside square brackets ''minus 4 ''colon''
 
'''primes''' ''inside square brackets ''minus 4 ''colon''

Latest revision as of 09:17, 8 June 2018

Visual Cue
Narration
Show Slide Title Welcome to the spoken tutorial on Manipulating Lists.
Show Slide

Objectives

In this tutorial, we will learn about
  • slicing and striding of lists
  • Sort and reverse lists.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisites

To practise this tutorial, you should know how to
  • run basic Python commands on the ipython console and
  • use lists

If not, see the relevant Python tutorials on this website.

We have already learnt about list and how to access individual elements in the list.
Show Slide


Slicing

Now we will see about slicing of lists.


The syntax for slicing is p inside square brackets start colon stop.


It returns all the elements of p between start and stop values.


The element with the stop index value will not be included.

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3


Press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Let us understand the slicing with an example.


Type, primes is equal to then type as shown.

Type

primes[4:8]

Now we will try to obtain all the primes between 10 and 20 from the above list of primes.


Type, primes inside square brackets 4 colon 8


Recall that the start index value is 0.


Observe that the first element we want is 11 which has index 4 in the list.


So, start value is 4.


Also, 19 is the last element we require which has index 7 in the list.


The element with index equal to stop value will not be included.


So, end value is 8.

Pause the video.


Try this exercise and then resume the video.

Show Slide Exercise 1 Obtain the primes less than 10, from the list primes.
Switch to terminal Switch back to terminal for the solution
Type

primes[0:4]

Type, primes inside square brackets 0 colon 4


Observe that in this case, our start value is 0.


7 is the last element we require which has index 3 in the list.


So, end value is 4.


<<PAUSE>>

Type num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] Next we will learn to use step value in slicing.


Type 0 to 13 in a list and assign it to a variable num as shown.

Type

num[1:10:2]


Press Enter

Let us say, we want all the odd numbers less than 10 from the list num.


We will specify the step value in which the slice must be obtained.


Type num inside square brackets 1 colon 10 colon 2


We have to start from element with index 1 upto index 10 in steps of 2.


It is called striding of list.


Press Enter to get the output.


We got the odd numbers less than 10.

Type

num[:10]


When no step is specified, it is assumed to be 1.


Similarly, there are default values for start and stop as well.


Type, num inside square brackets colon 10


It gives the first 10 elements of the list.


If we don't specify the start value, the first element of the list is taken as starting.

Type

num[10:]


Type, num inside square brackets 10 colon


This gives us all the elements from 10th element to the end.


If we don't specify the stop value, the elements till the last index of the list will be returned.


Start or end value can be negative to indicate that they are counted from the end of the list.

Type

num[::2]

Next let us get all the even numbers in the list "num"


Observe that all the even numbers are at even index locations.


So, type num inside square brackets colon colon 2


We got all the even numbers.


This is called striding.

Pause the video.


Try this exercise and then resume the video.

Show Slide Exercise 2 Obtain all the multiples of three from the list num.
Switch to terminal Switch back to terminal for the solution
Type, num[::3] Type, num inside square brackets colon colon 3


It gives us every third element from the list (i.e) multiples of 3.


<<PAUSE>>

Type a = [5, 1, 6, 7, 7, 10] Next let us learn to sort a list.


Type, a is equal to inside square brackets 5, 1, 6, 7, 7, 10

Type, a.sort() sort method is used to sort a list.


Type, a.sort open and close brackets

Type

a

Type, a to get the output.


We can see that the contents of the list a is sorted now.

Slide:

Sorted()

  • Python provides a built-in function called sorted.
  • sorted function sorts the list which is passed as an argument to it.
  • It returns a new sorted list.
Type a = [5, 1, 6, 7, 7, 10] Again we will assign the same value to a as shown.
Type sorted(a) Type, sorted inside brackets a
Type sa = sorted(a) We can store this sorted list into another list variable sa.


Type, sa is equal to sorted inside brackets a

Type sa To see the sorted list, type sa
Type, r = [1, 2, 3, 4, 5]


Python also provides the reverse method which reverses the list in place.


Type, r is equal to inside square brackets 1, 2, 3, 4, 5

Type r.reverse() r.reverse open and close brackets
Type r To see the list r, type, r


We got the reverse of the list.


The original list r is changed now.

Type, r = [1, 2, 3, 4, 5] To reverse a list, we can also use striding with negative values.


Again we will assign the same value to r as shown.

Type r[::-1] Type, r inside square brackets colon colon minus 1
Type ra = r[::-1] We can also store this new reversed list in another variable.


Type, ra is equal to r inside square brackets colon colon minus 1

Type ra To see the reversed list, type ra
Pause the video.


Try this exercise and then resume the video.

Show Slide

Exercise 3


Given below is the list of marks of a student in an examination.


Obtain a list with marks in descending order.

Switch to the terminal Switch back to the terminal for the solution.
Type marks = [99, 67, 47, 100, 50, 75, 62] Type the list of marks as shown.
Type,

sorted(marks)[::-1]

To get the marks in descending order type,

sorted inside brackets marks inside square brackets colon colon minus 1

Type

sorted(marks, reverse = True)

We can also get the same output by typing,

sorted inside brackets marks, reverse equal to True


<<PAUSE>>

Show Slide

Summary slide


This brings us to the end of this tutorial.


Let us summarize.


In this tutorial, we have learnt to,

  • Obtain the parts of lists using slicing and striding.
  • Sort lists using the sort method.
  • Use the method reverse to reverse the lists.
Show Slide

Evaluation


Here are some self assessment questions for you to solve


  1. Given the list primes. How do you obtain the last 4 prime numbers?
  2. Given a list p, of unknown length. Obtain the first 3 characters of it.
Show Slide

Solutions


And the answers,
  1. The last four primes can be obtained from the given list as,

primes inside square brackets minus 4 colon

  1. The first 3 characters can be obtained as,

p inside square brackets colon 3

Show Slide Forum Please post your timed queries in this forum.
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 Project 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, Nirmala Venkat, Priyacst