© Distribution of this video is restricted by its owner
00:01 | Hello everybody. This is a review for the topic of the week |
|
|
00:07 | This lecture is different from the one was given live over zoom but it |
|
|
00:13 | essentially the same topic. So if attended that lecture and got what was |
|
|
00:18 | it, you don't necessarily need to through this one. So so once |
|
|
00:23 | we're gonna talk about lists today. And before we get to list, |
|
|
00:30 | look at the big picture. In collection is a data type composed of |
|
|
00:37 | pieces. Any data type which has components is a collection. Examples of |
|
|
00:44 | . List things we have seen We'll see today and dictionaries. We |
|
|
00:50 | also cover in this course. There other collection types called couples and |
|
|
00:56 | We're not gonna cover them in this just to uh to manage the content |
|
|
01:04 | here. And the fact that you learn most of what you're going to |
|
|
01:09 | about collections from these three things that's dictionaries. So now the topic again |
|
|
01:16 | lists. And however before we get list, let's talk for a minute |
|
|
01:24 | strings. And the reason for doing is that uh it's in a lot |
|
|
01:31 | a lot of commonality between strings and and there are some really important |
|
|
01:37 | So it's good to have the context strings before we actually start talking about |
|
|
01:43 | . So a real quick summary. is a sequential collection but only of |
|
|
01:50 | . Individual characters can be accessed by . You can create over string over |
|
|
01:56 | elements of a string. And you check membership in a string with operations |
|
|
02:03 | and not in if a character is the string or not. We also |
|
|
02:08 | some other methods. Operations on These include concatenation or addition, |
|
|
02:15 | And strange just means basically combining the or concatenate in the strength reputation. |
|
|
02:23 | you use the multiply operator, you multiple copies of the string repeated in |
|
|
02:27 | new string comparison. It's a it's alpha numeric comparison less than equal and |
|
|
02:33 | on. We also saw the length lowercase converts a string from one to |
|
|
02:40 | other. You can count count usually the frequency of a character in a |
|
|
02:46 | . You can look for something in string and you have slices. So |
|
|
02:51 | is a general landscape. The kind operations 11 can do in strength and |
|
|
02:57 | see that many of these, most these have their equivalent enlists and it |
|
|
03:04 | not be identical because list is a kind of data structure. So finally |
|
|
03:13 | get to list themselves. List is sequential collection. It's a collection and |
|
|
03:20 | means there's an order about parliament, element and so on. And the |
|
|
03:25 | thing here is of python data Things were collection of characters, lists |
|
|
03:32 | a collection of python data items so is similarity. Um Both are |
|
|
03:39 | Both a recollection. However this is items. So and the data items |
|
|
03:45 | be any type. Uh They can integers, floats, characters or even |
|
|
03:52 | or even other lists can be the . So here's some examples. Uh |
|
|
03:59 | is a perfectly legal list. Er where each member of the list |
|
|
04:05 | just a character A L P H . This is another legal list where |
|
|
04:11 | member is an integer 01234 and so . Here's another example. This is |
|
|
04:19 | list with a bunch of elements and appear to be names of various kinds |
|
|
04:26 | animals and uh but each of them a strength. So essentially this is |
|
|
04:31 | example of a list whose individual data are our strengths and in each of |
|
|
04:40 | uh the type of individual elements was same here, it was characters |
|
|
04:47 | it was integers and here it was . However, that is not necessarily |
|
|
04:56 | case. So let's look as a example. This is also a perfectly |
|
|
05:01 | strength here. First element is zero the second is a floor, there |
|
|
05:07 | a string, another string and here have another list. So so basically |
|
|
05:15 | point is that the members of the can be any of these and they |
|
|
05:19 | be combinations of mix and match of various subtypes as well as a list |
|
|
05:27 | . Uh here we have an example a list is inside a list. |
|
|
05:35 | member of a list itself is a . The terminology we use for a |
|
|
05:40 | like that we have a nested A list within a list is a |
|
|
05:46 | list. And sometimes we refer to inner list. The whole thing is |
|
|
05:50 | list. And the this inner smaller is a sub list. That's just |
|
|
05:58 | . Um How do we create a list creation? Well, simple. |
|
|
06:05 | actual statements we saw in the previous , They're just repeated here. They're |
|
|
06:12 | just illustrations. They're perfectly legal python . So you can use these python |
|
|
06:19 | to um to create those lists. just saying str equals comma, |
|
|
06:27 | comma, comma. A will actually a list that has the name |
|
|
06:32 | S. T. R. And these members. Same for this |
|
|
06:35 | Same for this one. Same for one. So the simplest way way |
|
|
06:41 | think of creating a list is to list the elements with commas in |
|
|
06:46 | And uh just to be clear the square parenthesis and and with a square |
|
|
06:53 | and whatever is inside is separated by . Um We'll see there are other |
|
|
07:00 | of creating and manipulating lists and will to those two. Now you have |
|
|
07:07 | list. How do you get to elements of the list very similar to |
|
|
07:12 | . You get to the elements through . So here's the list for |
|
|
07:17 | let's say you want to get to element bird. So so the ordering |
|
|
07:24 | is similar to list for the This is index zero. This is |
|
|
07:29 | one. This is index 23 and . So if you wanted to get |
|
|
07:35 | bird, you would have to say and index one. So that will |
|
|
07:41 | you a bird. Now let's think another example let's say dog. And |
|
|
07:48 | you wanted to get to that you say pets 33 is the index for |
|
|
07:56 | . And that would give you this . But you can also say pets |
|
|
08:03 | two. If you remember there's an way of indexing strings and the same |
|
|
08:10 | over to list where the last element minus one, second, last is |
|
|
08:15 | two, then minus three minus four five and so on. So so |
|
|
08:22 | -2 as well as pets. Three give you dark if this is the |
|
|
08:27 | , pets. Okay, so very to strength. Uh moving on. |
|
|
08:36 | So let's let's look at some more . So this is the same, |
|
|
08:40 | know, complicated, Similar somewhat more list. Okay, so here we |
|
|
08:49 | mixed list too. Until sorry, element is always zero, next |
|
|
08:58 | next is two and you get 12.9 surprises here. Now what about mixed |
|
|
09:05 | three. So if you're watching the , pause for a second and see |
|
|
09:11 | do you think? Mixed list three be? And what should mix this |
|
|
09:15 | ? Should be. So you can and then restart essentially. Mixed list |
|
|
09:22 | Will not be false. It would this entire sub list containing false and |
|
|
09:29 | . Okay. And because the members the lesser 012 then this whole thing |
|
|
09:36 | three and then you go to four you go to five. Okay, |
|
|
09:41 | mixed list three then is uh this thing And makes less four should be |
|
|
09:52 | . Okay? So in other words in counts only list elements doesn't go |
|
|
10:05 | the surplus. When you're doing indexing one more times you don't want |
|
|
10:09 | three think of this as one that's three and then you go to |
|
|
10:14 | and five. Now obvious question, you want. What if you wanted |
|
|
10:22 | get access to these elements? There a way let's say for example you |
|
|
10:28 | to get access to the element we'll look at two ways of doing |
|
|
10:36 | which are basically similar one. Is first you assign this sub list to |
|
|
10:44 | variable, you just say a list max mixed list three and now at |
|
|
10:51 | point the variable a list will have value false summer doug. Okay, |
|
|
11:00 | it's straightforward. This is just another so you can and this is does |
|
|
11:05 | have a sub list, it's not . It's a plain simple list. |
|
|
11:09 | you can get to the element dot With LS one. Right? So |
|
|
11:15 | this is zero and this is one you say LS one and that will |
|
|
11:19 | you the string down. Okay, one more time. This is dark |
|
|
11:31 | uh oh sorry, step back, looking at an alternate approach for doing |
|
|
11:38 | the same thing that we just saw the previous example we want to get |
|
|
11:43 | to this list element dog. And making an explicit assignment to this sub |
|
|
11:52 | . So here mixed list three will you a false start. No |
|
|
12:00 | Same thing we've seen before. Now here there is a way to directly |
|
|
12:07 | the sublet list with a sub So here three is the index into |
|
|
12:13 | list. And assuming mixed with three a list then one is an index |
|
|
12:20 | sub list. Okay, so then would give you dark again. |
|
|
12:32 | so list gives you this list three within that indexing with one, this |
|
|
12:38 | 01 and that will give you Actually this kind of indexing pattern continues |
|
|
12:47 | further to the string. So now of this, what what will this |
|
|
12:52 | you? So think of it this . This all gives your dog. |
|
|
13:01 | . And dog. Index one is ? So that's no string. |
|
|
13:08 | this is just using indexing on In which case the I'm sorry |
|
|
13:15 | I'm going to transfer this zero over 00.0 is of course D. So |
|
|
13:20 | this whole indexing scheme goes to the and sublet list and then to the |
|
|
13:26 | so that will give you d So that is clear. First time your |
|
|
13:30 | you get the sub list, then can in second one. indexes inside |
|
|
13:35 | and you can continue if it's a or string inside you can index within |
|
|
13:42 | with going in from left to All right let's move one. This |
|
|
13:57 | the final results of what we discussed now. Let's move on to a |
|
|
14:05 | more common operations. Things you can on list. And again these are |
|
|
14:14 | similar to the operations you can do strength. So we'll talk about length |
|
|
14:20 | and not in. Yeah those. so length of mixed list is 12345 |
|
|
14:31 | six. So it's six. Again you're talking about length you do not |
|
|
14:36 | count a sub list as one Doesn't matter if it has two or |
|
|
14:42 | elements inside it. Okay so um just step back. Okay so over |
|
|
14:56 | what do we get here? So of mixed list three. So one |
|
|
15:02 | time. Mixed list three if you from left to right. Is this |
|
|
15:07 | string? Okay sorry this sub list a sub string. It's established. |
|
|
15:13 | list three is established. So length mixed list three is the number of |
|
|
15:19 | in this sub list and that's gonna too. Now some simple other queries |
|
|
15:26 | in mixed list. Is there an called bird in mixed list or a |
|
|
15:31 | . Bird in mixed Mixed list. course there is. So this should |
|
|
15:36 | true. Now what about animal in list. No there are animals but |
|
|
15:44 | isn't a string called animal. There's like that. So that is |
|
|
15:51 | Now is dark in mixed list. this is a little tricky question that |
|
|
15:58 | is uh a dog. Of course here, but it's not a member |
|
|
16:05 | the list. Mixed list. It's member of the sub list. That |
|
|
16:11 | the third element of the mixed So directly dog is not an element |
|
|
16:18 | mixed lists. Okay, so this be false. So just you can |
|
|
16:26 | check the dancers, we put our , what are here. And if |
|
|
16:39 | actually wanted to check if the dog there in this particular sub list, |
|
|
16:46 | you can specifically say is dog in the three. Which refers to this |
|
|
16:53 | this sublet list. And that of is going to be true because there |
|
|
16:57 | the string dog in there. All . A few more list operations. |
|
|
17:08 | much we're going over operations that we know in strings. And what do |
|
|
17:15 | mean in the context of lists? , here's the same example list. |
|
|
17:20 | list. Mixed list. 123. this is what's called slice. And |
|
|
17:27 | have seen slicing in the context of . So this would go from Index |
|
|
17:37 | . The part of the string part the list in this case, starting |
|
|
17:41 | one going up to three, but including three. Okay, so that's |
|
|
17:48 | uh slicing works. That's how it for strings. And that's how it |
|
|
17:53 | for less. So this is going be um Mix list 123. The |
|
|
17:59 | Mix this 123 is going to be list which contains these two elements. |
|
|
18:04 | that's here to answer it. So slicing we learned some about slicing it |
|
|
18:10 | carries over from strength to list here concatenation. So we have mixed list |
|
|
18:25 | carol plus the strength. Um okay so um plus cow ball. |
|
|
18:55 | this is the original mix list. add these elements and essentially it does |
|
|
19:02 | you expect. It's the same list before until cat. And there's these |
|
|
19:08 | elements just get added. Okay, pretty straightforward again, just like |
|
|
19:18 | you can add list one plus list . And it essentially would be a |
|
|
19:24 | list with elements of list one, followed by elements of list two. |
|
|
19:30 | . If you said I want a string. That string one plus string |
|
|
19:34 | Okay, reputation or multiplication. Once it works on less. Similar to |
|
|
19:42 | . You can say three, multiplied 123. And python would give you |
|
|
19:48 | list that is essentially 123123123 repeated three based on this number. So |
|
|
20:06 | So we learned in strings that strings not mutable. Okay, so if |
|
|
20:14 | had a string called 123456. So cannot say that I'm going I want |
|
|
20:27 | change some character in the string. the third character to eight or |
|
|
20:33 | That's just not legal. Okay so okay so just to make sure the |
|
|
20:41 | that you cannot do that, you change individual components of a strength. |
|
|
20:46 | that's what makes strength not mutable. mutability is a property where if you |
|
|
20:54 | change components of our collection. Lists are mutable. So which means |
|
|
21:01 | in a list if you wanted to one or the other character, it's |
|
|
21:05 | legal to do that. So the here says new list one Equals |
|
|
21:17 | Okay so Here new list one is element So you can change that element |
|
|
21:30 | uh to the new element 100. else stays the same. And you |
|
|
21:37 | a new list with the the element changed and uh everything is perfectly fine |
|
|
21:45 | legal. Okay, Moreover you cannot change one element, you can change |
|
|
21:54 | slice of a list. Okay here it's saying is to take the slice |
|
|
21:59 | the list, slice to 2 to and then replace whatever is there on |
|
|
22:06 | slice with just this is 200. . So let's see. 01 to |
|
|
22:14 | slice. 2 to 5 is gonna 234. Okay so this part of |
|
|
22:22 | list is gonna get replaced by the list 200. 300. So that's |
|
|
22:29 | it looked like. So you go this, replace those and you get |
|
|
22:33 | list so you can take a slice . You can also as a special |
|
|
22:40 | replace a slice with nothing with the list. Which is same as saying |
|
|
22:47 | rid of the slice. 2 to but not the whole list. Just |
|
|
22:52 | slice slice slice 2 to 5 need be removed. Okay so let's see |
|
|
22:58 | here. This is 01, this 234. So all these Need to |
|
|
23:07 | removed. And that would give you list. Just what is left here |
|
|
23:12 | just 1 to 100. So the point here is this a new |
|
|
23:17 | you can change individual elements and you sort of the common sense logical approach |
|
|
23:26 | how to work with indexing for updating of a list or slices of a |
|
|
23:37 | . There is also one more operation delete. So you can delete a |
|
|
23:46 | . Of course you could delete the list in which case they'll be |
|
|
23:50 | And you can also delete a slice a list. So delete list 2 |
|
|
23:56 | 40123. So it'll get rid of and you'll you'll end up if you |
|
|
24:04 | After that. This is what will printed. So it's essentially delete list |
|
|
24:10 | this life. Is this exactly the as saying list 2-4 is assigned an |
|
|
24:17 | list. But this seems cleaner. actually trying to get rid of a |
|
|
24:21 | of the list. So here you're say delete that over here you say |
|
|
24:28 | know it's just like a slice and it another value and the new value |
|
|
24:32 | to be empty but they're both equally . Um couple more methods that you |
|
|
24:42 | apply to list. Most of these something equal lint in strength list dot |
|
|
24:55 | adds an item to the end of list. In third you give it |
|
|
25:00 | position and you give it an item it will add a new item at |
|
|
25:07 | particular place and said list pop position it goes to the position in the |
|
|
25:15 | and removes that element that's there and that. And if you do not |
|
|
25:23 | this is empty then it would just the last item that's the default. |
|
|
25:29 | are methods list sort list is the and they do what you expect, |
|
|
25:35 | well sorted based on the numeric ordering we've talked about before and with human |
|
|
25:43 | read details and exactly what it especially if the the types are different |
|
|
25:48 | so on. And the list reverse reverse the list where element essentially the |
|
|
25:56 | element will become the first element. second last becomes the second element and |
|
|
26:00 | on. More matter. Um You say remove item. So over here |
|
|
26:11 | Just various list methods. Their methods remove an item let's say the item |
|
|
26:20 | a string cat. It will remove first occurrence of um the item Cat |
|
|
26:27 | the list, leave just to the alone index item. It'll look for |
|
|
26:33 | item, Cat and return the It won't do anything. Just return |
|
|
26:37 | position of where that item is. finally count item will return the number |
|
|
26:44 | occurrences of the item in this case cat in the list. So all |
|
|
26:49 | these are fairly intuitive methods that we . I believe we have seen in |
|
|
26:58 | context of strings. Also. All , changing topics or sub topics. |
|
|
27:08 | can iterate over the list. We that we could iterate over a |
|
|
27:13 | We can go all the elements of string. And in a very similar |
|
|
27:17 | we can iterate or a list. let's say list this here. |
|
|
27:24 | Bird, Cat, dog, So you can have a statement that |
|
|
27:28 | for name in animals. Print So what it's going to do is |
|
|
27:35 | with the first element of the That's bird. I'm gonna go and |
|
|
27:40 | printed then it will go to the one is Cat, Next one is |
|
|
27:45 | and next one is fox. And that point the list is complete and |
|
|
27:55 | iteration has gone over all the elements the list and the process will |
|
|
28:00 | Okay, so this would be what printed as a result of this kind |
|
|
28:06 | a code. Okay, So that's way to do it. We saw |
|
|
28:12 | ways of iterating uh in uh uh a string. And now we go |
|
|
28:20 | the second way for which there is an analog in list here. It |
|
|
28:25 | . So instead of saying name in , you can get similar results by |
|
|
28:30 | for I in the range and length animals. Okay, let's look at |
|
|
28:37 | for a second length of animals. is a list. Its length is |
|
|
28:45 | . So this would essentially be for in range four. Okay. And |
|
|
28:54 | know that range four. Italy it's values of I that we go |
|
|
29:00 | we're going to be 012 and So the cord in the loop says |
|
|
29:07 | animals with that index. So it print the first time. The element |
|
|
29:16 | index zero in the list. So this is the list animals. |
|
|
29:21 | element for the index zero is So it will print bird and next |
|
|
29:26 | it will be one, it will cat and dog and fox. Essentially |
|
|
29:30 | results will again be the same Cat, dog, Fox. These |
|
|
29:34 | codes will do um actually the same in this case, but depending on |
|
|
29:41 | you're trying to do in um in problems, one of the others would |
|
|
29:47 | more sense generally. Okay um over we've covered a bunch of things between |
|
|
30:01 | that we can do with lists and . And using those kind of operations |
|
|
30:07 | methods you can go from a string a similar looking list. For |
|
|
30:12 | you're given a list, a list these elements. And you could convert |
|
|
30:17 | to a string or you could be a string and you can convert it |
|
|
30:23 | a list. So this is something can try on your own. And |
|
|
30:29 | next slide does have one solution for , but we're not going to discuss |
|
|
30:36 | . Moving on a little note on range function. So we saw this |
|
|
30:41 | function as soon as we started on loops. And at that time we |
|
|
30:47 | said that, you know, you said range something, it'll go over |
|
|
30:51 | elements of its range five, it'll 01234. Range 125, it'll go |
|
|
31:00 | . So uh at the time, was some kind of um magic |
|
|
31:05 | but essentially range 1515 is just this . Okay, so, exactly if |
|
|
31:15 | say my list equals range 1 to , it's simply a list. So |
|
|
31:19 | iterations for over a list. nothing. There's nothing special about |
|
|
31:24 | It just produces a list. And you iterate over the list as you |
|
|
31:28 | iterate or any other list. So did not talk about it at the |
|
|
31:35 | because we did not know what lists when we were studying range, but |
|
|
31:41 | sort of demystify what the range function . It just creates a list and |
|
|
31:46 | you iterate over it. Now, couple of methods that are new in |
|
|
31:54 | sense that just about everything we saw far is uh has an analog in |
|
|
32:02 | between strings and less. So let's at a couple that are different. |
|
|
32:07 | is a method that converts a strength a list. So um it takes |
|
|
32:18 | string, it looks for a D , then breaks that string into pieces |
|
|
32:24 | on the d limiter. Uh And end result being that one string becomes |
|
|
32:30 | whole bunch of strings and that whole of strings is put together as a |
|
|
32:37 | . And this is actually very common text processing and it should be obvious |
|
|
32:42 | in a little bit. Now suppose have this uh strength. So this |
|
|
32:50 | is obviously text. It is a from a text but it could have |
|
|
32:55 | a whole paragraph or even a whole . And now I suppose you have |
|
|
33:00 | string and you want to get to words in this text. That is |
|
|
33:06 | want to do some analysis on word word that says not on the whole |
|
|
33:13 | . That could be who knows, be like I said a paragraph, |
|
|
33:16 | book. It could be really Um So so to get to the |
|
|
33:23 | from this text as a strength, can use the split function. So |
|
|
33:28 | split is essentially looking for a blank . So you already says a blank |
|
|
33:35 | and it breaks the string there. blank space. It breaks the string |
|
|
33:40 | , another blank space. It breaks string there, you end up with |
|
|
33:44 | smaller strings, which essentially the words text. And if you apply this |
|
|
33:52 | to the list dot split. The result is this list. It contains |
|
|
33:59 | of the strength and uh you saw it was called but without a parameter |
|
|
34:07 | an argument. And if you don't it an argument, it assumes blank |
|
|
34:12 | the argument that basically splits the string there is a blank. But in |
|
|
34:18 | you can split based on anything. let's look at another example, the |
|
|
34:24 | string. Now you want to split but the separator is character. I |
|
|
34:31 | not uh it's not blank anymore, a specific character. So not happens |
|
|
34:38 | that you start processing from left to and look for occurrences of I so |
|
|
34:45 | have this one, you have one and I think that's about it. |
|
|
34:52 | now it's gonna break the string into based on the occurrence of I. |
|
|
34:58 | you end up with something like This is the first piece. This |
|
|
35:03 | the second piece. This is the piece and those become the three strings |
|
|
35:08 | Split every place. There is an in the original string. And of |
|
|
35:12 | this could be anything. It doesn't to be uh if it's nothing it's |
|
|
35:17 | blank or it can be any So that's split correspondingly there's a function |
|
|
35:29 | join. Split takes a string as and converts it to a list. |
|
|
35:35 | . Now join does the opposite. takes a list and glues the element |
|
|
35:41 | the list together into a string. here the example is a list that |
|
|
35:48 | , fox gorilla and you're given a blue. Okay. Uh This is |
|
|
35:57 | string that needs to be inserted between pair of elements in the string. |
|
|
36:06 | if you say new string equals glued joint. Animals. The result is |
|
|
36:12 | string. I don't think we need . The result is a string which |
|
|
36:16 | all the elements of the original list together in the string but in every |
|
|
36:23 | of them you have the glue. . This glue could be colon |
|
|
36:28 | it could be a blank string and could be Hello and that's what will |
|
|
36:34 | inserted where our two elements of the are being combined the strength. So |
|
|
36:41 | can go from a list. A with split. You can go and |
|
|
36:47 | sorry. You can go from a to list with with a split and |
|
|
36:52 | can go from a list of string a joint. And both of these |
|
|
36:58 | useful in text processing. Especially I'd split. It's very common. |
|
|
37:06 | that's I think all the operations of that we're gonna go over. Of |
|
|
37:13 | there are many more and um but think this gets you pretty far and |
|
|
37:22 | course you can look up other operations as needed basis. Quick review so |
|
|
37:27 | we have one more important thing. List is a sequential collection of data |
|
|
37:34 | unlike strings were sequential collection of Data objects can be accessed by indexing |
|
|
37:42 | sub list can be accessed by So any part of a list can |
|
|
37:51 | accessed by slicing. And I think sort of forward use the word sublet |
|
|
37:57 | but here it just means that any of the string you can get to |
|
|
38:03 | by slicing operations. That usually with colon list of notable individual items are |
|
|
38:12 | of the list can be replaced by else with just an assignment statement. |
|
|
38:19 | we've seen that let's support a number methods. You can manipulate lists in |
|
|
38:26 | many ways and they will grow and as needed. Unlike a string which |
|
|
38:33 | either you delete it, make a one, you can just add or |
|
|
38:38 | Yeah you can't um just manipulate, can manipulate strings and you cannot manipulate |
|
|
38:46 | . You can combine strings into a string but you can manipulate and they |
|
|
38:51 | grow, they can get longer or can get short and in particular you |
|
|
38:57 | a and concatenate and delete operations to or remove from a list. So |
|
|
39:04 | a quick summary of the of uh and less operation so we're going to |
|
|
39:12 | gears and get to the major topic the last major topic for today. |
|
|
39:22 | um so let's get started suppose you statement acts equal string my name, |
|
|
39:32 | equal string? My name. Nothing so far. So python has two |
|
|
39:42 | here. It in in the computer's it can put this string my name |
|
|
39:52 | call it X. Okay. That's it has to do with the first |
|
|
39:58 | . When you the second one, has two options. It could just |
|
|
40:06 | create another string my name. And you have two strings in memory. |
|
|
40:18 | with the label acts, the other the label, Y. Or name |
|
|
40:22 | and with name Y. That seems know what most of us would think |
|
|
40:28 | happen in a scenario like this a worked. There is also the option |
|
|
40:33 | python to say when you get to equals my name. It says, |
|
|
40:39 | know, I've already got my name memory so I don't need to create |
|
|
40:47 | whole string again. What I'm going do is give it to names. |
|
|
40:54 | why an ex. Those points to same my name. You got the |
|
|
40:59 | string and memory with two names. this would be the two options. |
|
|
41:04 | it turns out you the way python is this one And not this one |
|
|
41:14 | it has something to do with the that the strings are not immutable. |
|
|
41:21 | therefore it's not like you can manipulate string and the others will have some |
|
|
41:27 | . So it's very simple. It , it doesn't cause any complications by |
|
|
41:31 | the string tune in. Okay, the answer here is when you have |
|
|
41:37 | code, there's only one um property the string in memory, which is |
|
|
41:42 | intuitive to most of us, but what happens. And this gives a |
|
|
41:45 | bit lead into the what we're going talk about strings next. Um how |
|
|
41:51 | you know if both of these are same or different strains? Now we've |
|
|
41:58 | this operator X equals Y. And just says that the value of acts |
|
|
42:03 | same as Y and whether it's the , you know, either of those |
|
|
42:09 | , this will always be true because X and Y have the value mining |
|
|
42:14 | python also provides another operator called S, Y. That is why |
|
|
42:22 | means that are they one and the thing. So in the memory, |
|
|
42:26 | the actual memory, if it's exactly same copy being pointed to by these |
|
|
42:32 | variables, then X. Is why true. And you can check that |
|
|
42:37 | write X equals minus vehicles, my and then uh do this check. |
|
|
42:45 | to print X is Y. And should find that it's true. |
|
|
42:49 | so the point is that when you X equals my name, Michael, |
|
|
42:54 | name. The A python actually just just one copy and and pointed to |
|
|
43:02 | from both from for X and Now changing gears. Now forget all |
|
|
43:10 | , that just gives you a little of a thinking on how memory management |
|
|
43:19 | naming and you you can make copies sometimes you don't have to make copies |
|
|
43:27 | but leaving that aside, let's get the list. So here Um you |
|
|
43:33 | the statement Alex equals 789 and uh would result in something like this, |
|
|
43:41 | is a list and you can think list as the container and you have |
|
|
43:48 | hanging from those containers and that's the in memory. What happens? LY |
|
|
43:54 | 789. Again, there are two . One is we could do what |
|
|
44:00 | did with the list where you just an another name and have only one |
|
|
44:08 | of the list in memory and the is to create another copy. So |
|
|
44:14 | see that python does the latter in case. So just to be very |
|
|
44:22 | , if you say a lexical L Y equals something, even if |
|
|
44:28 | contents are identical, you will find copies of the lists are created, |
|
|
44:34 | , Alex and L Y have nothing do with each other. Okay. |
|
|
44:43 | , it's a very different story if say Alexa equals L Y and instead |
|
|
44:49 | saying L Y equals 789, you l Y equals lx and that is |
|
|
44:56 | aliasing and this is what I thon you already have the strength from the |
|
|
45:04 | statement a lax which has those content three elements 789? It just makes |
|
|
45:13 | . Y. Points to the same . That means that the that same |
|
|
45:18 | has two names A lax and Y. No new list is |
|
|
45:22 | So the buzz word we use is saying L. Y. And Alexis |
|
|
45:27 | aliases for the same list. So you say Y equals Alex L. |
|
|
45:33 | becomes an alias of Alex or another of Alex or L. Y. |
|
|
45:38 | pointing to the same list as That's clear. So if that's the |
|
|
45:45 | then uh let's uh look what happens . Okay so you have Alex equals |
|
|
45:54 | . You say L. Y. Alex. So in that case what |
|
|
46:00 | was uh L. Y. Is to the same list. Now you |
|
|
46:07 | Alex one equals 148. What that is initially we didn't show it |
|
|
46:16 | This was it you said Alex one not eight but 108. And now |
|
|
46:25 | say print L. Y. Okay for a minute, what do you |
|
|
46:32 | will happen here? Okay so basically Alexis value, L. Y. |
|
|
46:40 | Alex. L. Y. Became of Alex. Then you changed |
|
|
46:47 | You never explicitly changed L. And then you try to print |
|
|
46:51 | Y. Which it doesn't appear, was changed after um it was assigned |
|
|
46:58 | value after this assignment L. Equals Alex. And somewhat counter intuitively |
|
|
47:08 | will get printed is 71089 not Even though L. Y. Calls |
|
|
47:15 | for 789. Election changes like uh did not change or it doesn't appear |
|
|
47:22 | changed. But the point is that this statement was done there are |
|
|
47:28 | Anytime you change an element of L. Y. Changes. Anytime |
|
|
47:32 | change an element of L. L. Exchanges, they're basically the |
|
|
47:36 | thing with two names. And here change Alex and print. L. |
|
|
47:41 | . And L. Y. Also the change that is it became one |
|
|
47:45 | it. So that's what happens when alias the list. Now. What |
|
|
47:53 | you don't want that? You want have a list Alex and you want |
|
|
47:59 | create a list. L. That has the same content but it's |
|
|
48:04 | different list. It's not the same . So you have to do something |
|
|
48:10 | . This is what we did last and Y equals Alex. You don't |
|
|
48:14 | that. It does not create a , it leaves only one copy and |
|
|
48:19 | an area. So we don't want do that. We want to create |
|
|
48:24 | new list from scratch whose elements are same as lx. So you can |
|
|
48:29 | it like this. Start a new . L. Y. That's an |
|
|
48:33 | list and then you run a for over all elements in Lx. So |
|
|
48:39 | for loop is 14. Lx. this value would be seven and eight |
|
|
48:45 | nine. Okay. And inside the you say L Y dot append |
|
|
48:52 | Okay, so when you do that have a list A lax. |
|
|
49:01 | sorry, L.Y. that is It has 789. But it is |
|
|
49:08 | different list. It's not a pointer uh it's not an alias of the |
|
|
49:17 | list. Okay, So that when are here you have two independent |
|
|
49:25 | Alex and L. Y. Which that point they are the same value |
|
|
49:29 | here. You say Alex one equals . That means here, what used |
|
|
49:34 | be? It becomes 108. And if you print L Y you get |
|
|
49:42 | because L Y was independent. Just Alex will not change and y in |
|
|
49:49 | case the list was blown. That to an independent copy was created. |
|
|
49:54 | the previous case there were alias. it's on one copy that uh that |
|
|
49:59 | being pointed to by two names Alex and L. Y. So |
|
|
50:03 | and uh aliasing are different. so to summarize a lexical 789, |
|
|
50:19 | Y equals Alex will create a essentially another list name pointing to the |
|
|
50:28 | value will create an alias. Uh you want to make a separate |
|
|
50:34 | one more alias is just giving another to the same list. Cloning means |
|
|
50:39 | a physical new copy. Another way creating a clone is to uh have |
|
|
50:47 | statement. So instead of doing the loop that we saw, you can |
|
|
50:51 | say as E equals lx. And that would, this would create a |
|
|
50:59 | list LZ with 789. So this a shortcut for saying make me a |
|
|
51:07 | list LZ whose elements are same as . So this is another simpler way |
|
|
51:14 | cloning the list, Alex. So is the simplest way of aliasing |
|
|
51:19 | giving it a new name. This the simplest way of cloning Alex. |
|
|
51:25 | every element of uh where you create new list L. Z, which |
|
|
51:29 | a copy of that list but it's . So where this these concepts are |
|
|
51:40 | important and relevant is parameter passing with list elements as parameters. So let's |
|
|
51:51 | a look. So there's a function . Fun. All it does is |
|
|
52:00 | takes um an argument and presumably the is a list. And all it |
|
|
52:06 | is element with index one in the . It changes it to some |
|
|
52:13 | 1 11. And after that it the list. Okay, that's |
|
|
52:19 | Take a list, change one number return it. Okay, now here |
|
|
52:25 | the code that is using it. more time. We say a |
|
|
52:30 | 789. Now we say L. equals uh whatever value is returned by |
|
|
52:40 | function. Okay, pretty straightforward, is a list. L Y. |
|
|
52:47 | what the function returns. Then we to print L. Y. And |
|
|
52:51 | we try to print Alex. So you print L. Y. As |
|
|
52:57 | expect, it would print seven. then remember uh this function had changed |
|
|
53:05 | the element 1 to 1 11 111 then nine. Now the question |
|
|
53:12 | what would print a lax print? think for a second, what would |
|
|
53:18 | print? So there's two options, is 789, the other is |
|
|
53:29 | And it turns out that this will 71119. Because when this list alerts |
|
|
53:39 | passed as a parameter, no new was created. So essentially the same |
|
|
53:47 | is being referred to inside the function if this whatever list is here is |
|
|
53:55 | alias for the list. So the , L par is the same |
|
|
54:02 | So when you change anything in l here, it also changes the corresponding |
|
|
54:09 | in L A X. So we that both of them would return 71129 |
|
|
54:19 | uh this is essentially what has is that the same list? Got |
|
|
54:24 | minutes. Okay, so this is of the more country intuitive but very |
|
|
54:35 | aspect of passing less sets parameters. . Hopefully that is clear. Now |
|
|
54:49 | see what if we did not want do that. What if we want |
|
|
54:53 | write a function? It takes a as parameter this, whatever it needs |
|
|
54:59 | do but does not change the parameters because the parameters will then go back |
|
|
55:08 | change the values in the main program unexpected with like what happened here, |
|
|
55:13 | call the function and suddenly your own is manipulated. So um this everything |
|
|
55:23 | this page is same as the previous except we've replaced. Um we have |
|
|
55:33 | statement local equal L par, Then change local instead of L par and |
|
|
55:39 | local. So remember what this does create a clone. So as soon |
|
|
55:45 | this function is called, we make copy of the argument or we clone |
|
|
55:52 | argument then then if you see that value of our parties never changed. |
|
|
55:58 | only thing that has changed is then it is returned. Okay, |
|
|
56:03 | , so if that is the you have a lexical 789, you |
|
|
56:08 | L y is equal to list fun lax in this case the execution goes |
|
|
56:14 | , it immediately makes a copy of and then manipulates the copy but does |
|
|
56:20 | change the original because of that. L Y will return um uh will |
|
|
56:28 | course return seven 1119, but it unchanged because this function did not change |
|
|
56:38 | argument just made a copy. So would still be 789. So um |
|
|
56:51 | this is how you can design a if you didn't want the the value |
|
|
56:59 | change, there's another way you could the same thing, basically the problem |
|
|
57:07 | looking at is that you have a . And uh so you want to |
|
|
57:14 | it as a parameter whatever is alleged things with it, but you don't |
|
|
57:18 | the original value to change. This one way the function itself makes a |
|
|
57:23 | before doing anything. I'm going back the previous page and uh I'm gonna |
|
|
57:36 | everything and show another way of So if you wanted the same |
|
|
57:44 | similar results where you have Alex, want to pass it to a function |
|
|
57:49 | then but not impact this. So way you can do, it would |
|
|
57:55 | have a statement here. L. equals a lax Poland that would create |
|
|
58:03 | clone of Alex called L. And then here instead of Alex, |
|
|
58:09 | say L. Y. Physical If you did that then Alex will |
|
|
58:15 | be changed. And when you try print Alex you will still get |
|
|
58:19 | Okay. All right. You may to spend some time on this. |
|
|
58:25 | is an um it's an important Okay, as a follow up to |
|
|
58:35 | discussion, we revisit a topic that are this that we started much earlier |
|
|
58:43 | functions called pure functions. So before talk about what's better and what's |
|
|
58:51 | let's get into the concept of a effect. Functions can have side |
|
|
58:56 | But what it means is that a can change variable values in the calling |
|
|
59:03 | . Okay, So you have a program, you call a function and |
|
|
59:11 | have X, Y, Z B. And function can also manipulate |
|
|
59:18 | correctly. Okay, so that's called side effect. When a function changes |
|
|
59:25 | in the main program and any function does not do that does not has |
|
|
59:30 | side effects is found appear function that's terminally like. So what functions can |
|
|
59:38 | are impure bookers before we get to . The way pure function communicates with |
|
|
59:47 | calling program is through parameters, gets important parameters but does not change them |
|
|
59:54 | it returns the value. So you the main program you call function and |
|
|
60:04 | has access to these but does not them. And when function is done |
|
|
60:11 | it returns its return value. And the only way for a pure function |
|
|
60:17 | make any impact on the program within program you might say X equals whatever |
|
|
60:23 | . But that is directly controlled by calling program. Not directly by the |
|
|
60:28 | . Okay. And the reason for talking about this is not just to |
|
|
60:32 | more buzzwords but generally speaking it's better have pure functions. Functions that do |
|
|
60:38 | have side effects. There is some that those functions are easier to write |
|
|
60:44 | to debug and improve productivity. So speaking, when you're writing a |
|
|
60:49 | especially with the list than the preferred is to write a function such that |
|
|
60:57 | um uh such that the list whatever passes a parameter itself is not |
|
|
61:06 | The function doesn't change the parameter. it needs to do some processing that |
|
|
61:12 | change the values, it makes a of whatever is passed as an |
|
|
61:16 | And then the final result, of is returned to the uh to the |
|
|
61:22 | program as the return value. Uh the end of this lecture. There |
|
|
61:30 | an assignment and uh you know, us the instructor of the T. |
|
|
61:36 | . If you have any questions about assignment and I'm going to stop |
|