More Complicated Data Types Recorded Transcript This transcript was created with a speech to text software. Please excuse any typos or mismatches with the video. Hi, everybody. Welcome back in this Python prep video. We're gonna talk a little bit more about more complicated data types. So, so far we've talked about in floats, bulls and strings. So let's talk about a couple more data types that are slightly more complicated but very useful when it comes time for us to do more data science stuff. So I'm gonna go ahead and share my Jupiter notebook, more complicated data types. Uh So in this notebook, we'll introduce some more complicated data types. Uh primarily lists, tools, sets and dictionaries. So a list is the collection of Python objects. So, so far we've talked about objects like ins floats and strings as well as some bulls. Uh So lists are collections of those objects that are mutable and we'll see that this means that they can be changed after they are created. So let's just start by making a list and then show you all the things that lists can do. So lists are made by taking your objects for instance an in or a string and then placing those objects within square brackets separated by commas. So each unique object. So number or string has to be separated by a comma. And then the notation or syntax is that those that uh set or that list of objects is separated by commas is surrounded by square brackets. So we'll execute this. And now we have a list denoted by these square brackets of strings. This comma is comma a comma list comma of comma strings or S T R S. Uh It doesn't just have to be the same uh object all the way through. It can consist of more than one kind of Python object. So for instance, I can have a list of a string, an integer, another string, even a list itself. So a list can have a list within it, lists of lists as well as boolean. So it doesn't have to be of a single type of Python object. It can be a list of many types of objects. So lists can be indexed. Uh So that means that each item here in this list will have a number or index corresponding to it that allows us to retrieve that particular item. Uh and just that item from the list without having to get other items. So in Python indexing starts with zero from the left and then moves forward. So the first entry of your list is actually in Python, the zeroth entry and so forth. Uh if you go from, you can index backwards. So starting from the right, you can start with a negative one and then the next one from the right is negative two and so forth. So before we show an example of this, I'm gonna take a quick aside and in the past couple of notebooks, if you've watched those videos, I've talked about variables. So variables are a way for you to take a data object in Python and then store it in something that you can call back to later. So uh for instance, think of this like in mathematics, right? We use X and Y so Y equals M X plus B, the variables there are Y and X Y and X stand in for uh any number of numbers. So we can kind of think of variables in Python as just a name that we're gonna give to a particular data object uh that we can then use to reference later. So as an example here, I'm making a floating point number 4.3 and I'm storing it in the variable X and to do that, you just say variable name for me here, it's X equal sign And then whatever you want it to equal. And so now that it's there any time I call X, the 4.3 should show up. OK. So we're gonna touch a little bit more on variables and how Python source thing is in your computers uh in the next notebook. But for now, I just want to have this quick aside because it's gonna make some of the stuff we do later with list, tool sets and dicks a little easier. Uh we're gonna touch uh to, to do so back to list. So let's see a list to see in action. So here is a list that I'm going to store in a variable called fruit basket. And I've called it that because it has apple, banana, grapes, Kiwi, lemon and grapefruit. So the first uh index, if we're indexing four words, the first index is zero, that's apple. The second index is one which goes to banana two goes to grapes, three goes to Kiwi, four goes to lemon and five goes to grapefruit. If we're gonna do it backwards, starting from the right, going to the left grapefruit gets a negative one, lemon gets a negative two grapes gets a negative or Kiwi gets a negative three grapes gets a negative four, banana gets a negative five and apple gets a negative six. So once we have that list and it's stored in a variable, we can index it and get the entries we'd like like. So, so you call the name of the list and then you do another set of square brackets. So square brackets here and then you put in the index that you're interested in. So for me, in this first example, I'm in interest interested in what's in the zero spot and I should get apple back. And as an example with reverse indexing, if I put a negative floor, uh if I'm interested in that, I should get grapes back. If I wanted to do the, the uh you know, the same one with apple, I would put negative six, I get apple back. OK. So let me go back to grapes. All right. So this is another just, you know, quick check. How are you comprehending things? Feel free to pause the video, work through these two code chunks and come back. I'm gonna work through them right now. So what is the third entry of fruit basket? So I can do print the third entry is. So remember the third entry would be in the second position. So fruit ... Basket and then at two ... and then the second entry from the right is I do fruit basket. And if I'm going from the right, the indexing starts at -1. So I want negative two. OK. Now it's saying index the list with one colon four, what is returned? So let's do fruit baskets, Square Brackets, 1:4. And so I can see here that I'm getting banana grapes and kiwi. And so what's going on here is in addition to indexing a single digit, you can also get consecutive entries of a list by going from the first thing in front of the colon. So here the one index which is banana up to the last index you give. So here up two means not including so one through four in Python speak will give me 12 and three. If I want to get the four, I have to do one through five. Ok. So when you do an index thing like this, I through J I is going to be the index of the item uh in the, that first gets returned and then J is the index of the item. Here I said of the last item, I'm gonna edit this really quick. J is the index of the item right after the last item. Uh And so, so in other words, ... I through J returns the items and positions ... I I plus one, I plus two dot dot dot J -2 J -1. Ok. So hopefully that makes it a little bit more clear, I'll update so that your notebook version already has this. Um I just figured this would help. OK. So here uh in addition, let's say you didn't want to stop. So let's say you just wanted everything from object two onwards. So you can do that just by doing I colon, you can similarly do the reverse where you want everything up to but not including object I by doing colon. I, so here I'm gonna get everything from item two onwards. So that is two would be grapes, Kiwi lemon grapefruit. Ok. ... So I said that lists are mutable and that means that I can change the entries as I please. And so what I can do now and then this is an exercise. But if you're not sure what to do, just feel free and watch right now, I can overwrite an entry in the list by finding that entry and then replacing it with something else. And so the, the syntax for that would be fruit basket, which is the name of my list, square brackets. I need to find the index for lemon, which is four. So fruit basket at ... four and I no longer want it to be lemon. I want it to be lime. So you just do equals and then the new thing you'd like to replace the old thing. And so now if I call fruit basket, there should be no lemons and one lime. OK. ... So Python has a number of built-in list functions just like it had a number of built-in string functions. So you can add an entry to the end of any list with dot Append. So if I have an existing list, I can call dot Append, put in whatever object I'd like to put in. So here I'm gonna add a plum to my fruit basket. And so now I have a plum. You can also add, let's say I have a whole another list of items I'd like to combine in an older list you can use dot Extend, whatever that other list is. So I call fruit basket dot extend. And here's my new uh list. It's gonna be pineapple, mango, coconut. That's what I want to add onto the end of my fruit basket. And so now I've got, after plum, I should see pineapple, mango, coconut. Uh, what happens when I run dot Sort? ... Well, let's see. ... And let's spell fruit basket correctly. So this is what it looked like before I ran dot Sort and I, after I ran dot Sort, we can see that. Now it's in alphabetical order. So apple, banana, coconut all the way down to plum. Ok. So as a quick aside, before we move on to tuples strings are similar to lists in the sense that they can be indexed. So here's a test string and I want you to uh probably pause this video or try and do it along with me and find the 4th and 7th letter in this string being mindful of Python's indexing uh rules. OK? So go ahead and pause the video now and I'll work on it right now. So we've got test string. We want the 4th and 7th letter. So that means we want the, the letter in the third position, the three position, ... We'll print that out so we can see it and then let's print out the letter in the 6th position. And so why? Because the fourth letter remember is in the third position because we start indexing at zero. So we should get out L and we should get out 1234567 I game. All right. So, and that a difference with lists is uh a difference between lists and strings is that strings are not mutable. So we couldn't do something like this. ... This uh won't work test string. Let's say I want to change the A into an I, I should, I let's see what happens. And we see I get an error because the string object does not support item assignment. This just means that once we have a string, we can't change the individual letters, uh using this sort of um approach like we could for a list. So lists are mutable, we can do this sort of thing. Strings are not mutable. And another item that is like the list but is not mutable is called a tuple. So a tule is like a list in that it's another collection of Python objects, but it's unlike a list in the sense that like a string, it's not mutable. So once we create a tuple, we can't change it. OK? Uh So here I'm gonna make a tuple called veggie tray. And the uh notation. The syntax for uh tuple is that you have a set of parentheses that are now highlighted in green and then whatever objects you're going to put in the tuple are separated by print or uh by comma. So here I have a tuple of strings. OK? Uh Similar to lists, it doesn't have to always be the same object just in this example. It is I could have had something like carrots too true. But I just chose to make four strings. And so now you're going to try and uh answer these questions. Uh and I'm gonna do them now but feel free to pause the video and then come back and check my answer. Ok. So we want the first entry from Veggie tray from the left. Well, that's just gonna be veggie tray at zero. And we also want the first entry from veggie tray from the right and that's veggie tray at negative one. OK. Uh Now we're gonna see what happens when I try and overwrite an entry. So Veggie tray car is in the two pos uh corn is in the two, the second entry. So it's in the one position. And remember I said this is not mutable, it's immutable. So what shall I see? An error? Two pole does not support item assignment. OK. Uh And then finally, we're gonna put Veggie tray inside of list, which is what I've done now. Uh And this will cast the string. ... So I've already written the answer out. I will make sure in your version that it's not already there. But we've got, you can see here that if we cast the tule as a list, we can now reassign, we can now overwrite something uh and change it. OK? OK. So we've got lists. We've got two poles. Another more complicated data uh object is a set. So a set works uh in a way very similar to the mathematical notion of a set. So it's a collection of distinct objects. So a set is made by placing distinct Python objects between a pair of brackets, uh curly brackets. I don't know what the proper name for them are. So, and then you separate it by commas. And so here we've got our curly brackets. Uh This comma is comma a comma sets. OK. We can see that once it's made, it doesn't maintain its order. Uh It's an unordered collection. Um And I believe Pythons orders them in a way that's optimal for memory storage. So let's look at the type of this and what we get back is a set. Uh We can also use the command set. So here's gonna be a grocery list that say you and your pal have made and your goal here is to take that grocery list and use these, you know, set casting. So just by putting the set around grocery list uh and get rid of um duplicates. OK? So feel free to do this on your own. I'm gonna do it right now. So you call set grocery list and you can see here for instance, apples and beef were repeated. But now in the set version, you only have one apples and one beef. So set is a nice way for taking something that has a lot of instances of something and maybe you just want to get the unique versions of them. So you can just call set ... uh sets. Also in addition to lists and tools have a number of useful functions. And some of the ones that I find most useful are the ones that just mimic uh actual mathematical set functions. So you can see what is common to both uh for to a pair of sets, you can see what is common by calling dot intersection. And so here I define set one, which is Apple computers and set two is as American apple and pie. And so what we can see is which uh items are in both lists. So to do that, you call set one. So a set object and you do dot intersection and then another set. So what this is telling Python is you want to see what is the intersection between the first set and the second set. And for us, it should just be Apple, ... you can now do the same thing. Maybe you're interested in what is in either of the sets we're looking at. So that's what's called the union. So is it in set one or set two? And again, it's the set dot union. ... Uh you can see what's in one set but not the other. And so for that, you, you just do a subtraction. So set one and then minus symbol, set two will find all the things in set one that are not in set two for us. That's gonna be computers. OK? All right. So a natural, if you're a math person, you're probably wondering, well, what's the notation for the empty set? And you might think that the most natural one is just to do a pair of brackets with nothing in it. But let's see what the type of that is. ... And it's a, a dictionary, it's addict. Uh And so that's a good segue into figuring out what dets are. So, what's a Python dictionary? This is a way to store information uh with keys and values. So you're gonna have a set of keys that correspond to a set of values where each key has its own particular value. So it's possible for one key and another key to have the same value. Um That's possible. Uh But one thing that isn't going to be possible is for there to be two keys of the same uh of the same type. So like you couldn't have two sets of keys that are both a uh each key that you use has to be unique. And so the idea here is like, you'll have a set of keys that you want to match to something. And I think it will become more clear once we make a couple of dictionaries. So for instance, here, this object that we were hoping and the earlier question might be the empty set is actually a dictionary. And so the way we can make a dictionary with stuff in it as you call uh similar to a set, you call these curly brackets and then your keys are always gonna go first. So here I have a key that is apple, the string apple and then a colon. ... And then the number one is the value associated with the key apple. So the notation is always your key followed by the value for that key comma another key followed by the value for that key comma and so on. So here we have the key apple is associated with the value one. The key as is associated with the value too, The Key American is associated with the value one And the key pie is also associated with the value one. So here this will just print it out and you can, you know, once you have a dictionary which we're gonna have stored in this practice dit variable. Uh Once you have that you can access the keys using dot keys and you can access the values which here are 1211 using dot values. OK? So if I call dictionary name dot keys, I get out the keys, dictionary name dot values, I get out the values. So if I want to get the value that's associated to a particular key, you just put in the name. So you do practice, died. So that's the name of my variable. Uh And then I put square brackets and then the key I want. So for us it'll be apple and I should get out of one. And I do uh similarly I could put, say as and I'll get two. Ok, let's go back to Apple. Uh And that is how dictionaries work. Ok. So I want you to look at these last two exercises, I'll do them right now. But if you need to pause the video, work through them on your own and come back when you're ready. Ok. So what we had dot Keys, which gave us the keys dot Values gave us the value. So what does dot Items give us? So practice dict that items. ... And so here now we have uh it says dict items, but inside which is the important part, we have a list of two poles. So each two pole has the key followed by the value key, followed by the value key, followed by the value and so on. So what that item's returns is dot Items returns a list of tuples of the form key and then comma value. Ok. So you code use a dictionary to store the ingredient amounts you need for an imaginary recipe. So we're gonna do the uh curly brackets and let's just say we've got some milk And we need one cup of milk. Maybe we'd need some eggs and let's say we need two eggs. Uh What about some flour? ... Maybe we need two cups of flour, uh maybe some sugar ... and we can do or cup and let's end it with some salt And let's do two tablespoons and then finally some baking soda and let's say that we need one tablespoon. ... So here's a recipe and it's a dictionary ... and uh there we go. OK. So that's it for this notebook. What we've learned in this notebook. Uh We learned about lists. We learned about two, we learned about sets and we learned about dictionaries. So now we have a pretty strong repertoire of data objects. In Python. we've got ins floats, uh booleans, strings, lists two poles, sets and dictionaries. Uh So that's a lot of data types. We now have the means to accomplish a lot of things. So in the next notebook, we'll start coding up uh using some standard computer science stuff. Uh Or we may do some more about variables. I'm trying to remember what the next notebook is. Uh But it's escaping me right now, but stay tuned for more great Python contents. I will see you in the next video and have a great rest of your day.