1 00:00:00,000 --> 00:00:03,420 Dictionaries in Python are incredibly important, 2 00:00:03,600 --> 00:00:07,700 and in other languages a dictionary is often called an object. 3 00:00:07,700 --> 00:00:10,600 So like in JavaScript or JSON, a dictionary is an object. 4 00:00:10,760 --> 00:00:13,490 And if you're familiar with those already, you'll be familiar 5 00:00:13,500 --> 00:00:18,150 with how it works in Python because they are very, very similar. 6 00:00:18,160 --> 00:00:21,120 But for the most part, you can really think of a dictionary 7 00:00:21,130 --> 00:00:24,540 like a variable with many variables inside of it. 8 00:00:24,700 --> 00:00:31,000 And so instead of saying 'kalob_name = "Kalob"', and 9 00:00:31,000 --> 00:00:37,430 then 'kalob_age = 30', and 'kalob_cats = 10 00:00:37,430 --> 00:00:42,300 2', and 'kalob_fav_food = "Taco"', 11 00:00:42,300 --> 00:00:45,270 instead of writing all of that, we can use this thing called 12 00:00:45,280 --> 00:00:48,300 the dictionary, and put this all inside of one variable called 13 00:00:48,310 --> 00:00:51,270 'kalob'. Now, of course, this doesn't have to revolve around 14 00:00:51,280 --> 00:00:54,000 me, it's just easy for me to make that example, but it could 15 00:00:54,000 --> 00:00:55,900 be around literally anything. 16 00:00:56,100 --> 00:00:59,070 And at this point in time, we're starting to actually sort 17 00:00:59,070 --> 00:01:01,300 of clump data together. 18 00:01:01,500 --> 00:01:03,450 So all of this has to do with me. 19 00:01:03,450 --> 00:01:06,900 So before we get started, let's go ahead and take a look at some syntax. 20 00:01:06,940 --> 00:01:09,750 So I got a variable here, and then I have this thing called a 21 00:01:09,760 --> 00:01:12,030 'key', and this other thing called a 'value'; 22 00:01:12,030 --> 00:01:17,500 and I could have another 'key' here, ':', and then another 'value' here. 23 00:01:17,560 --> 00:01:21,000 And so this 'key' should always be a string, ideally something 24 00:01:21,010 --> 00:01:22,500 that's easily accessible. 25 00:01:22,700 --> 00:01:25,460 I like to stick with the idea that your 'key' should always 26 00:01:25,470 --> 00:01:28,050 be named like your variables. 27 00:01:28,060 --> 00:01:33,560 So if we run this, and we check out 'person', we have 'key': 'value', 28 00:01:33,560 --> 00:01:38,900 'key2': 'value2', and 'type(person)' is going to be a 'dict', 29 00:01:38,900 --> 00:01:41,500 which is short for the word dictionary. 30 00:01:41,500 --> 00:01:46,100 Now, the nice thing about this, is if we wanted to get just 31 00:01:46,150 --> 00:01:48,960 a particular 'key', we could simply print out 'person', 32 00:01:49,280 --> 00:01:53,540 and then just like how we use indexes with a list or a tuple 33 00:01:53,540 --> 00:01:55,600 or a string, this would be our new index. 34 00:01:55,600 --> 00:01:57,400 But instead of using a number, we use the name. 35 00:01:57,400 --> 00:01:59,400 So let's go ahead and 'print 36 00:01:59,400 --> 00:02:01,500 [no audio] 37 00:02:01,500 --> 00:02:04,500 (person)', and then just like an index, 38 00:02:04,510 --> 00:02:07,120 but instead of a number, we use a string, 39 00:02:07,130 --> 00:02:10,180 and a string always has those quotations or apostrophes around it. 40 00:02:11,100 --> 00:02:13,699 Now, that doesn't exist, because obviously there's no 41 00:02:13,740 --> 00:02:16,350 'name' in here, or no 'key' called 'name'. 42 00:02:16,350 --> 00:02:22,600 But if we change this to 'key', we're going to see 'value'; 'key2' : 'value2'. 43 00:02:22,650 --> 00:02:24,840 So let's go ahead and work with a real example. 44 00:02:25,080 --> 00:02:28,620 So let's say there's a 'course', and this 'course' has a 'name', 45 00:02:29,600 --> 00:02:32,600 and this course is "Python for Everybody". 46 00:02:32,600 --> 00:02:35,600 Let's say this 'course' also has 'students'. 47 00:02:35,600 --> 00:02:37,500 How many students are there? I don't know, 48 00:02:37,580 --> 00:02:40,300 a hundred thousand students, 100_000, something like that. 49 00:02:40,300 --> 00:02:43,960 And in this 'course', how many 'lessons' are there, 50 00:02:43,960 --> 00:02:46,300 or let's not use an int, 51 00:02:46,300 --> 00:02:47,400 let's use a float. 52 00:02:47,620 --> 00:02:49,180 How long is this course? 53 00:02:49,190 --> 00:02:51,100 I actually have no idea at this point in time. 54 00:02:51,280 --> 00:02:54,700 So let's put 'length', and the 'length' of this 'course' is going 55 00:02:54,700 --> 00:02:58,200 to be I don't know, 8.75, we'll assume that's hours. 56 00:02:58,260 --> 00:03:01,650 Now, if we wanted to, we could actually get really, really 57 00:03:01,650 --> 00:03:09,110 interesting with this. We could say 'lessons' is now a list, and we could list of 58 00:03:09,110 --> 00:03:10,200 all the lesson names. 59 00:03:11,600 --> 00:03:18,700 So we could have 'Strings', 'Lists', 'Dictionaries', 'If Statements', 60 00:03:18,760 --> 00:03:21,840 we haven't got there yet, but we will, all sorts of stuff. 61 00:03:21,850 --> 00:03:23,910 We could have all that kind of data in there as well. 62 00:03:23,920 --> 00:03:27,240 And so really, any sort of data type can be your 'value', and 63 00:03:27,250 --> 00:03:30,770 your 'key' should always just be something that's easy to relate 64 00:03:30,780 --> 00:03:34,160 to, something that's easy to remember, like a variable name. 65 00:03:34,170 --> 00:03:35,450 So let's go ahead and run this. 66 00:03:35,600 --> 00:03:39,110 We'll see that 'course' is a bunch of information in here. 67 00:03:40,900 --> 00:03:42,400 And let's run just for fun 68 00:03:43,400 --> 00:03:44,400 'type(course)'. 69 00:03:44,720 --> 00:03:46,280 It's a dictionary, 'dict' as expected. 70 00:03:46,430 --> 00:03:51,480 But what if we wanted the type of 'lessons'? We could do 71 00:03:51,480 --> 00:03:58,800 'type(course['lessons'])', and remember, instead of using 0, 72 00:03:58,800 --> 00:04:04,200 like we would do for a string, or a list, or a tuple, we 73 00:04:04,240 --> 00:04:06,500 simply put in the string, 74 00:04:06,510 --> 00:04:09,230 and this 'lessons' here matches this one here. 75 00:04:09,240 --> 00:04:13,220 And this one when I hit 'Shift + Enter' is going to say it is a 'list' 76 00:04:13,500 --> 00:04:14,820 just like that. 77 00:04:14,830 --> 00:04:16,500 Now, why is this important? 78 00:04:16,500 --> 00:04:19,700 Well, because now we have all this information grouped together 79 00:04:19,700 --> 00:04:20,700 in one variable. 80 00:04:21,700 --> 00:04:25,600 So this no longer has to look like this. 81 00:04:27,200 --> 00:04:30,900 So it doesn't say 'course_name = "Python for Everybory"', 82 00:04:30,970 --> 00:04:33,330 'course_students = 100_000', 83 00:04:33,360 --> 00:04:36,270 'course_length = 8.75', 84 00:04:36,360 --> 00:04:40,150 'course_lessons = ['Strings', 85 00:04:40,160 --> 00:04:41,650 'Lists', 'Dictionaries', 'If Statements']'. 86 00:04:42,250 --> 00:04:45,040 We just have one variable to rule them all, which is really, 87 00:04:45,040 --> 00:04:46,090 really, really nice. 88 00:04:47,300 --> 00:04:49,900 And once again, just as an example, if you wanted to get 89 00:04:50,900 --> 00:04:55,100 any value in there, you could, with 'course['name']'. This is your 90 00:04:55,180 --> 00:04:56,520 variable name, and this is the 'key', 91 00:04:56,520 --> 00:05:00,800 and that 'key' is one of these - 'name', 'students', 'length', or 'lessons'. 92 00:05:02,400 --> 00:05:03,400 Sure enough, there it is. 93 00:05:04,100 --> 00:05:07,000 But what if we wanted to add something more to this? 94 00:05:07,060 --> 00:05:10,510 We know with the list, we can use 'list.append', but that's 95 00:05:10,520 --> 00:05:12,310 not going to work here because it's not a list. 96 00:05:12,500 --> 00:05:18,990 We want to instead say 'course', we just simply give it 97 00:05:19,000 --> 00:05:20,340 a new 'key' right off the bat. 98 00:05:20,460 --> 00:05:25,090 Let's give this a 'subject', and this is going to be 'Programming', 99 00:05:25,100 --> 00:05:28,800 hit 'Enter', and let's take a look at what it does. 100 00:05:28,880 --> 00:05:32,480 We have - 'name', 'student', 'length', 'lessons', and now we have the 101 00:05:32,490 --> 00:05:34,250 'subject'. It's a string of 'Programming'. 102 00:05:34,360 --> 00:05:37,900 And if we wanted to delete anything, this is actually pretty 103 00:05:37,900 --> 00:05:40,700 interesting as well, we could simply type the keyword 'd-e-l'. 104 00:05:40,700 --> 00:05:44,300 You can see that it's syntax highlighted here as 'del'. 105 00:05:45,400 --> 00:05:46,900 We give it 'course'. 106 00:05:46,900 --> 00:05:51,300 Let's get rid of 'length', but let's make sure we spell 'length' correctly. 107 00:05:52,800 --> 00:05:53,800 And then 'course'. 108 00:05:54,300 --> 00:05:59,900 And before we had 'length': 8.75, we deleted 'course['length'], 109 00:05:59,900 --> 00:06:02,000 and now it's not in here at all. 110 00:06:02,310 --> 00:06:05,860 Now, let's say you just wanted to get all of the keys in 111 00:06:05,870 --> 00:06:09,970 here, we could put a variable in here called 'keys', and do 112 00:06:10,400 --> 00:06:12,000 'course.keys'. 113 00:06:12,000 --> 00:06:14,800 This is a method on the 'course' object. 114 00:06:15,390 --> 00:06:17,860 And let's take a look at what 'keys' are, 115 00:06:18,070 --> 00:06:20,450 and it's a dictionary of 'keys'. 116 00:06:21,100 --> 00:06:26,400 But if you look inside of this, that looks a lot like a list, doesn't it? 117 00:06:26,460 --> 00:06:31,730 And what if you wanted all of the 'values'? You can do 'values 118 00:06:31,740 --> 00:06:32,930 = course.', 119 00:06:32,940 --> 00:06:34,850 and let's just hit tab, and see what we have here. 120 00:06:34,850 --> 00:06:37,200 'clear', 'copy', 'fromkeys', 'get', 121 00:06:37,200 --> 00:06:38,600 actually we'll talk about get in a second, 122 00:06:38,600 --> 00:06:42,400 I'm glad that one came up, 'pop', but let's go ahead and actually just get 'values'. 123 00:06:42,400 --> 00:06:44,700 [no audio] 124 00:06:44,700 --> 00:06:46,400 And let's see what 'values' are. 125 00:06:46,400 --> 00:06:48,800 And the 'values' again is a dictionary of 'values', 126 00:06:48,800 --> 00:06:52,200 but look at the innings of this, the inside. 127 00:06:52,240 --> 00:06:55,810 We have one item here, "Python for Everybody", 100000, 128 00:06:55,900 --> 00:06:58,300 the next one is a list inside of a list, 129 00:06:58,300 --> 00:07:01,900 and then our last item here is a string called 'Programming'. 130 00:07:01,920 --> 00:07:06,330 Now, just for fun, let's do a little more real life example 131 00:07:06,340 --> 00:07:11,370 here. So let's say I'm going to ask the user if they want 132 00:07:11,380 --> 00:07:13,020 information from this 'course'. 133 00:07:13,020 --> 00:07:16,300 They need to type in the 'name', 'students', 'lessons', or 'subject'. 134 00:07:16,300 --> 00:07:22,800 So let's say 'what_info_do_you_want =', 135 00:07:22,800 --> 00:07:26,900 and that's a terrible variable name, very verbose, a little 136 00:07:26,980 --> 00:07:31,550 long though, and say, "Enter name students 137 00:07:31,700 --> 00:07:33,300 lessons or subject". 138 00:07:33,360 --> 00:07:36,120 And then that's going to be stored in this variable. 139 00:07:36,120 --> 00:07:39,900 And what we can do now is say, the data or the 'value', 140 00:07:39,900 --> 00:07:44,500 let's call it 'data = course.get()', 141 00:07:44,500 --> 00:07:45,900 and then whatever this is, 142 00:07:45,900 --> 00:07:47,900 [no audio] 143 00:07:47,900 --> 00:07:49,100 and this is either going to be 144 00:07:49,170 --> 00:07:51,840 'name', 'students', 'lessons', or 'subject'. 145 00:07:51,850 --> 00:07:53,310 And then let's print that out. 146 00:07:53,310 --> 00:07:54,800 'print(data)'. 147 00:07:56,100 --> 00:07:57,900 "Enter name students lessons or subject" 148 00:07:57,990 --> 00:08:01,930 Let's type in 'students', gives us a 100000. 149 00:08:01,930 --> 00:08:03,000 Let's rerun this. 150 00:08:04,000 --> 00:08:06,100 Let's do 'subject'. 151 00:08:07,100 --> 00:08:08,100 'Programming'. 152 00:08:08,100 --> 00:08:11,700 So all this is doing, is saying, "Hey, this variable is essentially 153 00:08:12,060 --> 00:08:14,850 subject". That is actually exactly what it is. 154 00:08:14,860 --> 00:08:15,810 It's a string as well. 155 00:08:15,960 --> 00:08:19,770 So 'data = course.get("subject")' is going to go, "Okay. Well, 156 00:08:19,780 --> 00:08:22,380 you've got a dictionary here called 'course'. Get 157 00:08:22,390 --> 00:08:26,550 the key called 'subject', and whatever that value is, put it 158 00:08:26,560 --> 00:08:27,570 into your variable. 159 00:08:27,580 --> 00:08:29,670 So now we're getting super pythonic here. 160 00:08:29,680 --> 00:08:32,640 We're getting really into the nitty gritty of a programming 161 00:08:32,650 --> 00:08:36,840 language. Now let's see what happens when that key does not 162 00:08:36,850 --> 00:08:40,650 exist. When you have a malicious user who simply does not 163 00:08:40,659 --> 00:08:44,250 follow the rules, which is like 90% of the internet. 164 00:08:44,380 --> 00:08:47,289 So when someone says, "Enter name students lessons are subject", 165 00:08:47,299 --> 00:08:51,200 and I go "cats tacos". 166 00:08:51,200 --> 00:08:53,500 [no audio] 167 00:08:53,500 --> 00:08:54,500 Oh, look at that. 168 00:08:54,570 --> 00:08:55,520 It came up with 'None', 169 00:08:55,520 --> 00:08:59,270 and that's because when you use 'get', and it doesn't find the 170 00:08:59,280 --> 00:09:01,430 key that you're looking for, it's default, 171 00:09:01,430 --> 00:09:04,300 also its second parameter is 'None'. 172 00:09:04,320 --> 00:09:09,330 We could also set the default parameter here to be, 'THIS WASN'T 173 00:09:09,340 --> 00:09:18,160 FOUUUNNDDDD', and let's rerun the cell, "ANYTHING THAT ISN'T A REAL 174 00:09:18,370 --> 00:09:22,240 KEY". So this is going to basically error quote unquote, but 175 00:09:22,250 --> 00:09:26,640 it's actually going to say, "Hey, this key does not exist 176 00:09:26,640 --> 00:09:30,200 as 'name', 'students', 'lessons' or 'subject', 177 00:09:30,220 --> 00:09:31,840 and it's going to return, 178 00:09:31,850 --> 00:09:32,680 "THIS WASN'T FOUUUNNDDDD". 179 00:09:32,680 --> 00:09:33,900 So let's hit 'Enter'. 180 00:09:33,900 --> 00:09:37,700 This is what I entered, and this is what came back. 181 00:09:37,760 --> 00:09:40,880 Now, a little bit later, we're going to look at looping over 182 00:09:40,880 --> 00:09:43,300 a dictionary, I think we might as well bring it up 183 00:09:43,380 --> 00:09:45,650 now. We don't really know about loops, but you know what, 184 00:09:45,660 --> 00:09:49,160 I like to pre-expose my students to some more advanced stuff, 185 00:09:49,160 --> 00:09:51,500 and then once we get to it in a few lessons, you'll be like, 186 00:09:51,500 --> 00:09:53,100 "Oh, this is somewhat familiar". 187 00:09:53,320 --> 00:09:57,280 So we can actually do this quite easily in a for loop. 188 00:09:57,700 --> 00:10:07,200 'for item,', no let's not do 'item', 'key, value in course.items()', 189 00:10:07,200 --> 00:10:10,900 and what this is going to do, is basically turn this into a list. 190 00:10:10,960 --> 00:10:14,770 So we've got a 'name' and a 'value', or a 'key' and a 'value'. 'key': 191 00:10:14,920 --> 00:10:16,900 'value' - 'name': 'Python for Everybody'; 192 00:10:16,910 --> 00:10:22,200 'key': 'value' - 'students': 100000; 'key': 'value' - 'lessons': list of lessons, 193 00:10:22,200 --> 00:10:27,600 and we can print 'key' with a fat arrow, '=>', 194 00:10:27,600 --> 00:10:29,400 just so we know that this is a little bit custom here, 195 00:10:29,400 --> 00:10:36,440 'value'. Let's run that. 'name', 'students' is 100,000, 'lessons' is 196 00:10:36,500 --> 00:10:38,900 a list, and 'subject' is 'Programming'. 197 00:10:38,960 --> 00:10:42,260 So for this last part, this particular one here, you do not 198 00:10:42,270 --> 00:10:44,600 need to know this, but I thought it would be fun to sort 199 00:10:44,610 --> 00:10:46,640 of show you how we can do it, because that's going to be 200 00:10:46,650 --> 00:10:48,440 a question that a lot of people are asking. 201 00:10:48,450 --> 00:10:51,050 I know when I was learning Python, that was one of my questions 202 00:10:51,060 --> 00:10:52,250 about dictionaries as well. 203 00:10:52,440 --> 00:10:56,310 So as a quick summary, dictionaries prevent this. 204 00:10:56,310 --> 00:10:59,800 So instead of having four variables, you have one variable. 205 00:10:59,800 --> 00:11:02,000 It's still four lines of code or more, 206 00:11:02,000 --> 00:11:06,200 but instead of 'kalob_name', 'kalob_age', 207 00:11:06,200 --> 00:11:08,410 'kalob_cats', 'kalob_fav_food', you 208 00:11:08,410 --> 00:11:10,800 could just have one variable called 'kalob'. 209 00:11:10,800 --> 00:11:12,800 [no audio] 210 00:11:12,800 --> 00:11:15,300 The type of a dictionary is 'dict'. 211 00:11:15,900 --> 00:11:19,100 If you want to print out a particular value, you just have 212 00:11:19,100 --> 00:11:21,100 to print out the dictionary name, 213 00:11:21,100 --> 00:11:24,300 so that variable name, and then the 'key', and this one matches 214 00:11:24,830 --> 00:11:26,010 right here. 215 00:11:26,100 --> 00:11:28,400 'value2' matches down here, 'value2'. 216 00:11:29,180 --> 00:11:32,300 This is a good example of a bigger dictionary, which has 217 00:11:33,100 --> 00:11:36,100 a string, an int, a float, and a list. 218 00:11:36,140 --> 00:11:39,920 We said that was still a dictionary. But if we wanted to 219 00:11:39,930 --> 00:11:42,650 get just the 'lessons', well that was originally a list, and 220 00:11:42,660 --> 00:11:44,270 when we run 'type' on it, it's a 'list', 221 00:11:44,330 --> 00:11:46,940 so we know that we can have dictionaries with different data 222 00:11:46,940 --> 00:11:47,800 types inside of it. 223 00:11:47,800 --> 00:11:51,970 We added a new item to our dictionary by simply saying the 224 00:11:51,970 --> 00:11:55,100 variable name, and then the key name is equal to the value, 225 00:11:55,800 --> 00:11:57,400 and that simply adds it in here for us. 226 00:11:57,400 --> 00:11:58,600 And that did it right there. 227 00:11:58,640 --> 00:12:03,350 We deleted an item out of our dictionary with the 'del' keyword, 228 00:12:04,800 --> 00:12:06,600 and that got rid of 'length' for us. 229 00:12:06,630 --> 00:12:08,610 8.75 no longer shows up 230 00:12:08,610 --> 00:12:09,600 if you look down here. 231 00:12:09,600 --> 00:12:11,500 [no audio] 232 00:12:11,500 --> 00:12:13,200 You can get a list of all the keys 233 00:12:13,290 --> 00:12:17,120 by running your dictionary, '.keys'. You can get a list of all 234 00:12:17,120 --> 00:12:20,800 the values by running your dictionary '.values'. 235 00:12:20,840 --> 00:12:25,040 And you can get a specific item out of your dictionary by 236 00:12:25,050 --> 00:12:26,810 running the '.get' method with, 237 00:12:26,990 --> 00:12:30,390 let's say we wanted to get the 'name', but if for whatever 238 00:12:30,400 --> 00:12:33,270 reason, this was deleted or this was never set, it can return 239 00:12:33,270 --> 00:12:35,900 a default, and that default is always 'None'. 240 00:12:35,910 --> 00:12:39,320 And last but not least, we did some weird thing that we probably 241 00:12:39,330 --> 00:12:41,870 don't fully understand yet, but it's still kind of cool. 242 00:12:42,110 --> 00:12:46,070 We looped through every key and value pair together in a 243 00:12:46,070 --> 00:12:47,780 for loop and printed them out. 244 00:12:48,020 --> 00:12:50,510 Now, you don't need to remember all of this right now. 245 00:12:50,520 --> 00:12:54,130 Again, this comes with a lot more experience, and that experience 246 00:12:54,140 --> 00:12:55,150 is built over time. 247 00:12:55,160 --> 00:12:57,850 So don't feel like you need to know this before continuing 248 00:12:57,860 --> 00:13:02,000 on. But what I would like you to do is create your own dictionary, 249 00:13:02,180 --> 00:13:06,270 add a new item to it, delete an old item to it, and then 250 00:13:06,270 --> 00:13:08,900 just print out your slightly changed dictionary. 251 00:13:08,940 --> 00:13:11,340 Once you've done that, let's head on over to that 252 00:13:11,340 --> 00:13:13,400 next lesson, we'll learn about another data type.