Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Concept: Dictionaries

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's quickly review dictionaries, as that's going to be
0:02 the focus of the rest of this chapter.
0:05 To create a dictionary, we can either use curly braces or we
0:08 can use this dict().
0:11 Just like we can also use list, instead of square bracket.
0:14 Like we did in the demo, we can create a dictionary
0:16 with the curly braces.
0:17 We can create a dictionary by saying dict.
0:20 Now you might wonder if we can also create sets
0:23 using curly braces, and we say curly brace.
0:25 What do we get?
0:26 Well, it's decided that dictionaries are more common
0:28 and without items in there, you can't distinguish
0:31 are there pairs of items, or individual?
0:33 So this also means create dictionaries.
0:34 We could also create them populated like this.
0:37 Using keyword arguments, so dict Bill equals two
0:41 Zoey equals 7, Michael equals 4.
0:43 That's the exactly the same in terms of the outcome
0:46 as writing what we have right here, so same idea.
0:49 If we want to get one of the elements out, like if we want
0:52 to know the value that Zoey has, then maybe we have
0:55 a variable, right now it has a value Zoey.
0:58 So we could print wins by name, so Zoey R, whatever the
1:02 value comes out.
1:03 So D bracket, that key, you're going to pull out that value.
1:06 The key is Zoey.
1:07 The value is 7.
1:08 So it's going to print, wins by Zoey R 7.
1:10 Maybe some capitalization and punctuation as usual
1:13 going on here, but, you know this is what the code does.
1:16 We also saw that if you say d bracket a key, and the key is
1:20 not in the dictionary, it doesn't just not give
1:21 you something back, it crashes.
1:23 So you might not want that.
1:25 You might want to use this safer version of .get
1:28 and then pass the name.
1:29 So dot get name, give us wins, if there's wins, we can print
1:32 there are seven wins.
1:34 Otherwise, we can say this player's never played
1:36 a game with us.
1:37 Okay, so that's a useful way, if you're unsure
1:39 if data is in the dictionary.
1:41 These are super powerful.
1:42 It takes a little while to get your mind around them.
1:45 But once you do, they're really, really great to work with.