Data structures
- [Instructor] We just looked at simple variables that were assigned a single value. But a single value like five or true or false is only so useful. We might want to have an array of values in a variable. So Python has something called data structures, and the first one we're going to look at is a list. So if we say my list is equal to open square bracket closed square bracket, this defines an empty list. And then we can add some values to it with commas, so this is a list of length four. And if we print my list, we see that printed out there. Now we can put really anything we want in a list. So you could have list of strings, and this is a list of length three containing three different strings. Alright, we could also have a list of mixed strings and integers and Booleans and whatever we want. False. We can even throw in another list in there. In fact, you can have a list of lists. Let me just do that. Okay, so we have a list of lists. And we can put values inside some of these inner lists, and put a false and a true, whatever we want. So there's a nifty function you have with lists called length. Okay, so if I look at the length of my list, I get three. And so that's this list here. And even though we have lists of lists inside of here, the length of this list is three, because there are three lists inside of it, so it has a length of three. And let's look at a couple data structures that are very similar to lists. And the first of these is called sets. So let's talk about sets. A set is almost identical to a list, except that all of the elements in it have to be unique. So we can define a set with curly brackets like this. And let's put some elements in it. And if we print it out, my set, we see those all get printed out with the curly brackets that indicate that it's a set. We can also call the type function. Of course it's a set. And we can also use the length function. So it has a length of five, there are five elements in the set. Now let's make a set equal to one one one two two, and look at the length of that. The length is two. Remember how I said that every element in a set needs to be unique? So when you add the elements one and one to the set, it just takes in one, doesn't add the second one. And then, similarly, discards that second two. So if we print my set, there's only those two elements there. And the other thing to know is that the order of elements in a list is very important. So, I can say one comma two equals one comma two, and that's true. So these are two lists, these are two equal lists. But if I do that, it's false, because the order of elements in the second list is different. But if I do this with a set. Two comma one. It's true, the order of elements in sets does not matter. I can even put a three in here. It's still true, and I can do a couple more ones. Still true, these are the same sets. Now let's look at tuples. So tuples are declared with parentheses, like this. One two three. And tuples are very similar to lists. They have a length, you can use the length function. Let's call that my tuple, singular. And length of my tuple is of course three. The order also matters with tuples. So, one comma two is not going to be equal to two comma one, that's false. The difference with tuples is that I can't append or add things to tuples. So if I do my list dot append four, and print my list, we're using that list from up top, it adds the four to it. And I can append a six, adds that on, whatever I want. But if I call my tuple dot append four, tuple object has no attribute append. You cannot modify tuples. We're going to learn a lot of ways to modify the values and lists, but once a tuple is declared, you can't change it, add to it, change any of the values in it, it has to remain the same. So why would you use tuples? Well, remember back to how computers store lists in memory. If the computer knows that you can potentially add things onto a list, that list is going to increase in size, and so Python will try and sort of pre-allocate a larger chunk of memory for that list than it needs in the moment. With tuples, Python knows that you're never going to be able to add to them. So it uses only exactly the amount of memory it needs to store tuples. So tuples are often used to, say, store X Y coordinate pairs. You could store many thousands or millions of X Y coordinate pair tuples much more efficiently in memory than you could with lists. Alright, and the final data structure we're going to look at is the dictionary. And this is very different than the previous three, but you'll use dictionaries a lot. So, remember dictionaries from the real world, big books, you look up words in them. And when you look a word up, you get back a definition of that word. And in Python, dictionaries are kind of like that. So let's make a dictionary here. Apple is going to be a red fruit. Bear is going to be a scary animal. Okay, and let's use my dictionary. And look up the definition of apple. Okay, so you can access the data in dictionaries like this, and you see it prints out the value of red fruit, this is the definition or the value that you get back for what we call the key. And dictionaries are declared with these curly brackets, a bit like sets. And you have these key value pairs inside of them. The keys in dictionaries also have to be unique, so if we take this dictionary, and, say, add a second definition to it. "Sometimes a green fruit." And then we call my dictionary and get the definition for apple, it actually just takes that second definition. So this is kind of interesting, because sets also have to have unique values. The order with sets, both sets and dictionaries, doesn't matter, you can put these keys in any order you want. So I've always thought that dictionaries were a little bit like sets. And of course they're both defined with the curly brackets. We're going to be using all of these data structures a lot throughout this course, this is just a quick introduction. So have fun, play around with these, and I'll see you later.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。