Python for Absolute Beginners Transcripts
Chapter: Course conclusion
Lecture: Review: Data structures
0:00 The next things that we talked about were data structures. And we had started with code that looked like this, on the left.
0:07 And remember, our dog, was not impressed. Not even a little bit. Because that is some bad code that is hard to maintain hard to evolve
0:15 and much, much more. We went into all the details there. We saw though, that important data structures that are built into Python
0:21 lists and dictionaries in particular allow us to build much cleaner code. And what's really pretty amazing is we made our game way more complicated
0:31 by just changing the definition of that dictionary or even the JSON file that was loaded into a dictionary
0:37 without even touching the code that operated a game. I mean that's really, really impressive. So, couple things we can do with dictionaries.
0:45 We can create them. We can create them with their name their class name, like this. Or we can create them with these curly braces, like so.
0:53 We can even create them pre-populated like here we're creating a dictionary that has 2 wins for Bill, 7 for Zoe, and 4 for Michael.
1:01 That's equivalent to creating it like this as well where you say, as a string key: value. key: value. So on. If we want to get a value back out, like
1:10 how many times has Zoe won? We're going to have a key. And usually this is not hard coded like, quote Zoe.
1:17 It's some variable that you've gotten its value assigned to. And then we can say well whoever's value is in name
1:23 they have however many wins we'd get out. Now remember, we must have that value as a key or it's going to crash. But for Zoe we have her, so it's says
1:33 wins by Zoe are seven. If we don't have Zoe in the dictionary this is going to throw a key error and crash and we'll be out of luck.
1:40 Our program's going to stop. Not going to be impressive. So what we can do is we can attempt to get the value out of the dictionary with a .Get
1:48 instead of the square brackets and pass the key. What we get back is either a value for the wins
1:53 or none in this case if there's no value for that name. In this case, we're actually leveraging the truthiness of wins.
2:02 If it comes back as None, going to be False. If it comes back as 0, also no wins. Going to be false, we'll say no games played.
2:10 Otherwise, it's going to come back and say there's one, two, three, five wins, so on. Dictionaries also list those are the two most important
2:17 data structures that we worked with throughout this course.