diff options
Diffstat (limited to 'manipulating_lists/script.rst')
-rw-r--r-- | manipulating_lists/script.rst | 263 |
1 files changed, 158 insertions, 105 deletions
diff --git a/manipulating_lists/script.rst b/manipulating_lists/script.rst index 4b4bbf5..b8727a9 100644 --- a/manipulating_lists/script.rst +++ b/manipulating_lists/script.rst @@ -19,26 +19,32 @@ Script ------ -{{{ Show the slide containing the title }}} +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -Hello friends. Welcome to this spoken tutorial on Manipulating Lists. +Hello friends and Welcome to the tutorial on 'Manipulating Lists'. -{{{ Show the slide containing the outline }}} +{{{ Show the slide containing objectives }}} -We have already learnt about Lists in Python. In this tutorial, -we will learn about more advanced features of Lists in Python like how -to concatenate two lists, details of slicing and striding of lists, -methods to sort and reverse lists. +At the end of this tutorial, you will be able to, -{{{ Shift to terminal and start ipython }}} + 1. Concatenate two lists + #. Learn the details of slicing and striding of lists + #. Sort and reverse lists. -To begin with let us start ipython, by typing:: +{{{ Switch to the pre-requisite slide }}} - ipython +Before beginning this tutorial,we would suggest you to complete the +tutorial on "Getting started with Lists". -on the terminal +{{{ Open the terminal and start ipython }}} -We already know what Lists are in Python, how to access individual +let us start ipython on our terminal +:: + + ipython + +We have already learnt about lists in Python, how to access individual elements in the list and some of the functions that can be run on the lists like ``max, min, sum, len`` and so on. Now let us learn some of the basic operations that can be performed on Lists. @@ -46,193 +52,240 @@ the basic operations that can be performed on Lists. We already know how to access individual elements in a List. But what if we have a scenario where we need to get a part of the entire list or what we call as a slice of the list? Python supports slicing on -lists. Let us say I have the list:: +lists. Let us say I have the list, +:: - primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] To obtain all the primes between 10 and 20 from the above list of -primes we say:: +primes we say +:: - primes[4:8] + primes[4:8] This gives us all the elements in the list starting from the element -with the index 4, which is 11 in our list, upto the element with index 8 -in the list but not including the eigth element. So we obtain a slice -starting from 11 upto 19th. It is a very important to remember that -whenever we specify a range of elements in Python the start index is -included and end index is not included. So in the above case, 11 which -was the element with the index 4 was included but 23 which was the +with the index 4, which is 11, upto the element with index 8 +in the list but not including the eighth element. So we obtain a slice +starting from 11 upto 19th. It is very important to remember that +whenever we specify a range of elements in Python, the start index is +included and end index is not included. So in the above case, 11, which +was the element with the index 4, was included but 23 which was the element with index 8 was excluded. -Following is an exercise you must do. +Pause the video here, try out the following exercise and resume the video. -%% %% Obtain the primes less than 10, from the list ``primes``. +{{{ Show slide with exercise 1 }}} -Please, pause the video here, do the exercise and then resume. + Obtain the primes less than 10, from the list ``primes``. +Switch to the terminal for solution + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - primes[0:4] + primes[0:4] + +It give us the primes below 10. -will give us the primes below 10. +{{{ Show the slide containing p[start:stop] }}} Generalizing, we can obtain a slice of the list "p" from the index -"start" upto the index "end" but excluding "end" with the following -syntax +"start" upto the index "end" but excluding "end" with the +syntax ``p[start:stop]`` -{{{ Show the slide containing p[start:stop] }}} +{{{ Switch to terminal }}} By default the slice fetches all the elements between start and stop including start but not stop. So as to say we obtain all the elements between start and stop in steps of one. Python also provides us the functionality to specify the steps in which the slice must be -obtained. Say we have:: +obtained. Say we have +:: - num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] + num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] If we want to obtain all the odd numbers less than 10 from the list ``num`` we have to start from element with index 1 upto the index 10 in -steps of 2:: +steps of 2 +:: - num[1:10:2] + num[1:10:2] When no step is specified, it is assumed to be 1. Similarly, there are default values for start and stop indices as well. If we don't specify -the start index it is implicitly taken as the first element of the -list:: +the start index, it is implicitly taken as the first element of the +list +:: - num[:10] + num[:10] This gives us all the elements from the beginning upto the 10th -element but not including the 10th element in the list "num". Similary -if the stop index is not specified it is implicitly assumed to be the -end of the list, including the last element of the list:: +element but not including the 10th element in the list. Similarly +if the stop index is not specified, it is implicitly assumed to be the +end of the list, including the last element of the list +:: - num[10:] + num[10:] -gives all the elements starting from the 10th element in the list -"num" upto the final element including that last element. Now:: +This gives all the elements starting from the 10th element in the list +"num" upto the final element including that last element. - num[::2] -gives us all the even numbers in the list "num". +To get all the even numbers in the list "num", we do +:: -Following is an exercise that you must do. + num[::2] -%% %% Obtain all the multiples of three from the list ``num``. +Pause the video here, try out the following exercise and resume the video. -Please, pause the video here. Do the exercise and then continue. +{{{ Show slide with exercise 2 }}} -:: + Obtain all the multiples of three from the list ``num``. - num[::3] +{{{ Show slide with Solution 2 }}} -gives us all the multiples of 3 from the list, since every third -element in it, starting from 0, is divisible by 3. + ``num[::3]`` gives us all the multiples of 3 from the list, since every +third element in it, starting from 0, is divisible by 3. The other basic operation that we can perform on lists is concatenation of two or more lists. We can combine two lists by using the "plus" operator. Say we have -{{{ Read as you type }}}:: +:: - a = [1, 2, 3, 4] - b = [4, 5, 6, 7] - a + b + a = [1, 2, 3, 4] + b = [4, 5, 6, 7] + a + b When we concatenate lists using the "plus" operator we get a new -list. We can store this list in a new variable:: +list. We can store this list in a new variable,say c, +:: - c = a + b - c + c = a + b + c It is important to observe that the "plus" operator always returns a new list without altering the lists being concatenated in any way. We know that a list is a collection of data. Whenever we have a -collection we run into situations where we want to sort the -collection. Lists support sort method which sorts the list inplace:: +collection, we run into situations where we want to sort the +collection. Lists support ``sort`` method which sorts the list in place +:: - a = [5, 1, 6, 7, 7, 10] - a.sort() + a = [5, 1, 6, 7, 7, 10] + a.sort() -Now the contents of the list ``a`` will be:: +Now the contents of the list ``a`` will be +:: - a - [1, 5, 6, 7, 7, 10] + a -As the sort method sorts the elements of a list, the original list we had -is overwritten or replaced. We have no way to obtain the original list -back. One way to avoid this is to keep a copy of the original list in -another variable and run the sort method on the list. However Python -also provides a built-in function called sorted which sorts the list -which is passed as an argument to it and returns a new sorted list:: +As the ``sort`` method sorts the elements of a list, the original list +we had, is overwritten or replaced. We have no way to obtain the +original list back. One way to avoid this is to keep a copy of the +original list in another variable and run the sort method on the list. +However Python also provides a built-in function called sorted which +sorts the list which is passed as an argument to it and returns a new +sorted list +:: - a = [5, 1, 6, 7, 7, 10] - sorted(a) + a = [5, 1, 6, 7, 7, 10] + sorted(a) -We can store this sorted list another list variable:: +We can store this sorted list into another list variable +:: - sa = sorted(a) + sa = sorted(a) -Python also provides the reverse method which reverses -the list inplace:: +Python also provides the ``reverse`` method which reverses +the list in place +:: - a = [1, 2, 3, 4, 5] - a.reverse() + a = [1, 2, 3, 4, 5] + a.reverse() -reverses the list "a" and stores the reversed list inplace i.e. in "a" -itself. Lets see the list "a":: +the ``reverse`` method reverses the list "a" and stores the reversed +list in place i.e. in "a" itself. Lets see the list "a" +:: - a - [5, 4, 3, 2, 1] + a But again the original list is lost. -.. #[punch: removed reversed, since it returns an iterator] -To reverse a list, we could use striding with negative indexing.:: +To reverse a list, we could use striding with negative indexing. +:: - a[::-1] + a[::-1] We can also store this new reversed list in another list variable. -Following is an (are) exercise(s) that you must do. + Pause the video here, try out the following exercise and resume the video. -%% %% Given a list of marks of students in an examination, obtain a - list with marks in descending order. - :: +{{{ Show slide with exercise 3 }}} - marks = [99, 67, 47, 100, 50, 75, 62] + Given a list of marks of students in an examination, obtain a + list with marks in descending order. + marks = [99, 67, 47, 100, 50, 75, 62] -Please, pause the video here. Do the exercise(s) and then continue. +Switch to terminal for solution. +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - sorted(marks)[::-1] + sorted(marks)[::-1] OR :: + + sorted(marks, reverse = True) - sorted(marks, reverse = True) +{{{ Show summary slide }}} +This brings us to the end of this tutorial. In this tutorial, +we have learnt to, + 1. Obtain parts of lists using slicing and striding. + #. Concatenate lists using the ``plus`` operator. + #. Sort lists using the ``sort`` method. + #. Use the method ``reverse`` to reverse the lists. + +{{{Show self assessment questions slide}}} + +Here are some self assessment questions for you to solve + +1. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, + 29]``, How do you obtain the last 4 primes? -{{{ Show summary slide }}} -This brings us to the end of another session. In this tutorial session -we learnt +2. Given a list, p, of unknown length, obtain the first 3 (or all, if + there are fewer) characters of it. + + +3. ``reversed`` function reverses a list in place. True or False? + + +{{{solution of self assessment questions on slide}}} + +And the answers, + +1. The last four primes can be obtained from the given list as, +:: + + primes[-4:] + +2. The first 3 characters can be obtained as, +:: - * Obtaining parts of lists using slicing and striding - * List concatenation - * Sorting lists - * Reversing lists + p[:3] -{{{ Show the "sponsored by FOSSEE" slide }}} +3. False. The function ``reverse`` will reverse a list in place. -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +{{{ Show the thank you slide }}} -Hope you have enjoyed and found it useful. +Hope you have enjoyed this tutorial and found it useful. Thank you! |