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