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