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