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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Timing !Narration |- | 0:00 | Hello friends and Welcome to the tutorial on 'Manipulating Lists'. |- | 0:05 | At the end of this tutorial, you will be able to, # Co…')
 
Line 28: Line 28:
 
|-
 
|-
 
|  0:35
 
|  0:35
| 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 <tt>max, min, sum, len</tt> and so on.  
+
| We have already learn't 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.  
  
 
|-
 
|-
Line 40: Line 40:
 
|-
 
|-
 
| 0:57
 
| 0:57
| But what if we have a scenario where we need to get a part of the entir e list or what we call as a slice of the 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?
  
 
|-
 
|-
Line 80: Line 80:
 
|-
 
|-
 
| 2;36
 
| 2;36
| Obtain the primes less than 10, from the list <tt>primes</tt>.
+
| Obtain the primes less than 10, from the list primes .
  
 
|-
 
|-
Line 96: Line 96:
 
|-
 
|-
 
| 3:00
 
| 3:00
| Generalizing, we can obtain a slice of the list "p" from the index "start" upto the index "end" but excluding "end" with the syntax <tt><nowiki>p[start:stop]</nowiki></tt>
+
| Generalizing, we can obtain a slice of the list "p" from the index "start" upto the index "end" but excluding "end" with the syntax p[start:stop]  
  
 
|-
 
|-
Line 116: Line 116:
 
|-
 
|-
 
|  3:59
 
|  3:59
| If we want to obtain all the odd numbers less than 10 from the list <tt>num</tt> we have to start from element with index 1 upto the index 10 in steps of 2
+
| 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
  
 
|-
 
|-
Line 164: Line 164:
 
|-
 
|-
 
|5:48
 
|5:48
| Obtain all the multiples of three from the list <tt>num</tt>.
+
| Obtain all the multiples of three from the list num  
  
 
|-
 
|-
Line 172: Line 172:
 
|-
 
|-
 
|5:59
 
|5:59
|<tt><nowiki>num colon colon 3</nowiki></tt> gives us all the multiples of 3 from the list.
+
| num colon colon 3 gives us all the multiples of 3 from the list.
  
 
|-
 
|-
Line 224: Line 224:
 
|-
 
|-
 
| 7:22
 
| 7:22
| Lists support <tt>sort</tt> method which sorts the list in place
+
| Lists support sort method which sorts the list in place
  
 
|-
 
|-
Line 236: Line 236:
 
|-
 
|-
 
|  7:46
 
|  7:46
| Now the contents of the list <tt>a</tt> will be
+
| Now the contents of the list a will be
  
 
|-
 
|-
Line 244: Line 244:
 
|-
 
|-
 
|  7:52
 
|  7:52
| As the <tt>sort</tt> method sorts the elements of a list, the original list we had, is overwritten or replaced.
+
| As the sort method sorts the elements of a list, the original list we had, is overwritten or replaced.
  
 
|-
 
|-
Line 280: Line 280:
 
|-
 
|-
 
|  8:59
 
|  8:59
| Python also provides the <tt>reverse</tt> method which reverses the list in place
+
| Python also provides the reverse method which reverses the list in place
  
 
|-
 
|-
Line 292: Line 292:
 
|-
 
|-
 
|  9:16  
 
|  9:16  
| the <tt>reverse</tt> method reverses the list "a" and stores the reversed list in place 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"
  
 
|-
 
|-
Line 360: Line 360:
 
|-
 
|-
 
|11:17
 
|11:17
| Concatenate lists using the <tt>plus</tt> operator.  
+
| Concatenate lists using the plus   operator.  
  
 
|-
 
|-
 
|11:20
 
|11:20
|3. Sort lists using the <tt>sort</tt> method.
+
|3. Sort lists using the sort method.
  
 
|-
 
|-
 
|11:22
 
|11:22
|4. Use the method <tt>reverse</tt> to reverse the lists.
+
|4. Use the method reverse to reverse the lists.
 
   
 
   
  
Line 378: Line 378:
 
|-
 
|-
 
| 11:30
 
| 11:30
|1. Given the list primes, <tt><nowiki>primes is equal to 1, 3, 5, 7, 11, 13, 17, 19, 23, 29 </nowiki></tt>, How do you obtain the last 4 primes?
+
|1. Given the list primes, primes is equal to 1, 3, 5, 7, 11, 13, 17, 19, 23, 29 , How do you obtain the last 4 primes?
  
 
|-
 
|-
Line 386: Line 386:
 
|-
 
|-
 
|11:53
 
|11:53
|3.<tt>reversed</tt> function reverses a list in place. True or False?
+
|3. reversed function reverses a list in place. True or False?
  
 
|-
 
|-
Line 410: Line 410:
 
|-
 
|-
 
|12:20
 
|12:20
| <nowiki>p within square brackets colon 3.</nowiki>
+
| p within square brackets colon 3.  
 
+
 
|-
 
|-
 
|12:24
 
|12:24
Line 418: Line 417:
 
|-
 
|-
 
| 12:28
 
| 12:28
| The function <tt>reverse</tt> will reverse a list in place.
+
| The function reverse will reverse a list in place.
  
 
|-
 
|-

Revision as of 16:08, 26 March 2013

Timing Narration
0:00 Hello friends and Welcome to the tutorial on 'Manipulating Lists'.
0:05 At the end of this tutorial, you will be able to,
  1. Concatenate two lists
  2. Learn the details of slicing and striding of lists
  3. Sort and reverse lists.
0:17 So, before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
0:24 So let's start ipython on our terminal
0:28 Type ipython and hit enter.
0:35 We have already learn't 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.
0:48 Now let us learn some of the basic operations that can be performed on Lists.
0:54 We already know how to access individual elements in a List.
0:57 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?
1:07 Python supports slicing on lists.
1:10 Let us say I have the list, primes equal within bracket 2,3,5,7,11,13,17,19,23,29.
1:33 Hit enter
1:35 To obtain all the primes between 10 and 20 from the above list of primes we say primes[4:8]
1:51 This gives us all the elements in the list starting from the element with the index 4, which is 11, upto the element with index 8 in the list but not including the eighth element.
2:04 So we obtain a slice starting from 11 upto 19th.
2:08 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.
2:18 So in the above case, 11, which is element with the index 4, was included but 23 which was the element with index 8 was excluded.
2:29 Pause the video here, try out the following exercise and resume the video.
2;36 Obtain the primes less than 10, from the list primes .
2:45 So now switch to terminal for solution.
2:47 Type primes within brackets 0 colon 4 and hit enter.
2:57 It give us the primes below 10.
3:00 Generalizing, we can obtain a slice of the list "p" from the index "start" upto the index "end" but excluding "end" with the syntax p[start:stop]
3:17 So by default the slice fetches all the elements between start and stop including start but not stop.
3:24 So as to say we obtain all the elements between start and stop in steps of one.
3:31 Python also provides us the functionality to specify the steps in which the slice must be obtained.
3:37 So we have num is equal to within brackets 0,1,2,3,4,5,6,7,8,9,10,11,12,13.
3:59 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
4:12 So we type num within square brackets 1 is to 10 is to 2 that is ,1 colon 10 colon 2
4:24 When no step is specified, it is assumed to be 1.
4:28 Similarly, there are default values for start and stop indices as well.
4:34 If we don't specify the start index, it is implicitly taken as the first element of the list
4:46 So type num within square brackets colon 10.
4:52 This gives us all the elements from the beginning upto the 10th element but not including the 10th element in the list.
4:59 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
5:10 So type num within square brackets 10 colon.
5:21 This gives us all elements starting from the 10th element in the list "num" upto the final element including the last element.
5:30 To get all the even numbers in the list "num", we do num in square brackets colon colon 2
5:42 So now pause the video here, try out the following exercise and resume the video.
5:48 Obtain all the multiples of three from the list num
5:56 The solution is on your screen.
5:59 num colon colon 3 gives us all the multiples of 3 from the list.
6:05 Since every third element in this starting from zero is divisible by 3.
6:14 The other basic operation that we can perform on lists is concatenation of two or more lists.
6:22 We can combine two lists by using the "plus" operator.
6:26 Say we have a is equal to within square brackets 1,2,3,4, b is equal to within square brackets 4,5,6,7
6:42 Then type a plus b and hit enter.
6:47 When we concatenate lists using the "plus" operator we get a new list.
6:52 We can store this list in a new variable,say c,
6:58 So c is equal to a plus b.
7:01 Then type c.
7:06 It is important to observe that the "plus" operator always returns a new list without altering the lists being concatenated in any way.
7:13 We know that a list is a collection of data.
7:16 Whenever we have a collection, we run into situations where we want to sort the collection.
7:22 Lists support sort method which sorts the list in place
7:28 So type a is equal to 5,1,6,7,7,10.
7:40 Then type a dot sort and closing brackets.
7:46 Now the contents of the list a will be
7:50 Type a and hit enter.
7:52 As the sort method sorts the elements of a list, the original list we had, is overwritten or replaced.
8:00 We have no way to obtain the original list back.
8:03 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.
8:10 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.
8:20 We can store this sorted list into another list variable
8:26 so type a is equal to 5,1 6,7,7,10 and hit enter
8:39 then type sorted within brackets a, enter
8:47 Now,as we said we can store the sorted list into another list variable.
8:52 So type sa is equal to sorted within brackets a.
8:59 Python also provides the reverse method which reverses the list in place
9:03 so type a is equal to brackets 1,2,3,4,5
9:10 a dot reverse within closing brackets.
9:16 the reverse method reverses the list "a" and stores the reversed list in place i.e. in "a" itself. Lets see the list "a"
9:27 So type a and hit enter.
9:30 But again the original list is lost.
9:32 To reverse a list, we could use striding with negative indexing.
9:37 So type a within square brackets colon colon -1.
9:45 We can also store this new reversed list in another list variable.
9:49 Pause the video here, and try out the exercise and resume the video.
9:54 Given a list of marks of students in an examination, obtain a list with marks in descending order.
10:01 marks are 99, 67, 47, 100, 50, 75, 62.
10:09 So now switch to terminal
10:17 and type marks is equal to within square brackets 99,67,47,100,50,75,62.
10:36 Now, in terminal type sorted within brackets marks then in square brackets colon colon minus 1.
10:54 Then or you can use another method
10:57 So sorted within brackets marks comma reverse is equal to True.
11:08 So this brings us to the end of this tutorial.
11:11 In this tutorial, we have learnt to,
11:14 obtain parts of lists using slicing and striding.
11:17 Concatenate lists using the plus operator.
11:20 3. Sort lists using the sort method.
11:22 4. Use the method reverse to reverse the lists.


11:26 Here are some self assessment questions for you to solve
11:30 1. Given the list primes, primes is equal to 1, 3, 5, 7, 11, 13, 17, 19, 23, 29 , How do you obtain the last 4 primes?
11:41 2. Given a list, p, of unknown length, obtain the first 3 (or all, if there are fewer) characteristics of it.
11:53 3. reversed function reverses a list in place. True or False?
11:58 Let us look at the answers
12:03 And the answers are
12:05 1. The last four primes can be obtained from the given list as,
12:09 primes within brackets -4 colon.
12:14 and 2. The first 3 characters can be obtained as,
12:20 p within square brackets colon 3.
12:24 and the third and the final question the answer is False.
12:28 The function reverse will reverse a list in place.
12:32 Hope you have enjoyed this tutorial and found it useful.
12:35 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha