Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Demo: Data structure basics (dicts, sets, lists)
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Before we dive into working with our rock, paper, scissors game and these data structures let's begin by just working
0:07
with the data structures in isolation so we can focus on that. So over here in our GitHub repository we had CH06 and here's our game we built.
0:15
I made a copy of that over here but I also made a empty folder called simple dit. In here, we're going to put some Python file
0:21
that we can work with in on macOS only there's a cool little bonus you can drag and drop this here and it will automatically open that up.
0:28
On Windows or Linux, you have to Save file Open directory and browser. Not a lot harder but it is a little bit.
0:34
So we're going to come over here and add a new file just call playground and in this playground I want to introduce you to some of the data structures.
0:43
The one that's most important is this thing called the dictionary. Oh it looks like we got to add a Python interpreter.
0:53
Now that we can run our little playground because we set up Python for some reason and gotten lost, no big deal
0:59
we're going to play with this idea of dictionaries. So we have a couple of different data structures.
1:04
First one that we're going to work with is dictionaries. We also have this idea of lists or arrays. We've already seen those.
1:11
We worked with those. Those are like [1, 1, 1, 1, 7, 11]. Something like that and then a third one is going to be sets. Sets, lists, and dictionaries.
1:19
These are three that are built into Python that are super, super useful. So we're just going to quickly go over the lists and sets
1:26
because we're actually going to focus on dictionaries. Now the lists we've seen that we can have let's just do lst like that.
1:33
Don't use the full proper list because list is the name of the variable or the class that you create so we want to not get in the way of that.
1:41
We might have just that thing I wrote there. Let's put 'em in a different order. We can print out the lists. It knows how to print itself and so on.
1:49
Let's see what that looks like. We can see that. It also has cool features like all sorts of stuff you can do. We can append an item to it like 15
1:57
and then if we print it again you'll see now it's added this item. We can also say list.remove 11. let's print that out again. Now 11 is gone.
2:06
We can also say, actually let's pend to just something so it's not in order. We can also say list.sort and then print the list one more time
2:15
and that'll sort it. So there's all sorts of cool stuff and then we saw that we can use these in a for in loop.
2:19
But these are lists here, very, very common. Sets are also simple and interesting. So a set is something like this. Let's go copy that.
2:30
It's like this except for the way we create it is with curly braces. Can also create dictionaries with curly braces but when you have individual items
2:38
not pairs of items in here then it's a set and the point of a set is it's a collection of items like this.
2:45
And you can loop over it and you can print it out but the difference is it's about distinct items. It has one and only one of them.
2:53
If I go over here and say set I can add one. I can do that three times. I can even add 11. But what you see come out when you run it
2:59
is 1, 11, and 7 because it doesn't matter how many times one is in there you just want to know is one in there, yes or no?
3:07
So this is looking for distinct items in a set without duplication. Those are very interesting. The third one and the one we're going to use the most
3:15
in this section here is something called a dictionary. A dictionary is kind of like a set but what it does is it lets you give a name
3:23
or what's called key and then quickly look up whatever value you associate here. So let's say we're going to have just d for dictionary
3:31
then here we can say the key is going to be Bob and how many times Bob has won a game and Sarah, how many times Sarah has won the game.
3:39
Initially when they start playing or whatever they're not going to have any wins. So I can come down here and I can print out
3:44
how many times Bob won by saying like this. I can go to the item and put a key in here and it should come out with whatever that is.
3:51
Here is the 0 and we can also update when I come down here and say += 1. Print it again. Now Bob has won once and if we print out the whole dictionary
4:00
you can see now it has Bob is 1 and Sarah is 0. If an item, something like this, doesn't exist and you want to add to it, you could say this:
4:09
Michael, I'm going to cheat and start out at seven. How about that? But can add me in there and if I run this, what do we get? Michael is seven okay.
4:17
This is really useful. We'll be able to do things like come over here and we'll be able to have wins or defeated by.
4:24
And here you can put interesting stuff like I could put a set. It doesn't have to be a primary or scale or numbers like this.
4:32
Suppose this is the dictionary for rock. Well what is rock defeated by? Rock is defeated by paper and it defeats scissors, okay.
4:40
And it could if we had like wolf, you know, we had that example when we had sponge or whatever we wanted to extend this like so
4:47
then you can see we can just add to it. So if we wanted to know hey what are you defeated by? Put a little f-string here.
4:57
Now if we run it, you can see you are defeated by wolf and paper. Pretty darn cool. So we're going to be able to use this data structure
5:04
really, really easily and to sort of gather up and encode in the data structure instead of code itself the rules of our game, and then we can save
5:13
these data structures to a file which will be really, really cool. Final thing I want to show you is if I get something that's not in the dictionary
5:21
we get a key error, fully crashes our program. If you're unsure if something is in the dictionary you can use this keyword get
5:28
and we can print that out and you just get none back. If you want to say, Oh you know if the thing is not there
5:34
give me 42, otherwise give me the value, you can use this more advanced version. So use this style if you're sure that stuff is there
5:41
or you want to write to it. Use this style if you're unsure if it's there and you want to check, Hey is that none or is it something?
5:47
Okay so these are the three main data structures of Python. We have the dictionaries. We have lists, similar to arrays in other languages
5:55
but they're called lists here. And then we have sets. Lists are for getting items in order, keeping them in order and accessing them.
6:03
And we can get the third one by saying list bracket two. Remember zero-based sets are about distinct elements. They do intersections and differences
6:10
and stuff like that. And finally dictionaries are given a key. I want to find value quickly.