1 00:00:00,000 --> 00:00:02,856 [MUSIC PLAYING] 2 00:00:02,856 --> 00:00:05,003 3 00:00:05,003 --> 00:00:06,420 SPEAKER: Well, hello, one and all. 4 00:00:06,420 --> 00:00:09,000 And welcome to our short on dictionaries. 5 00:00:09,000 --> 00:00:10,790 Now, dictionaries are useful when you want 6 00:00:10,790 --> 00:00:15,510 to store similar kinds of information in terms of these key-value pairs. 7 00:00:15,510 --> 00:00:19,560 So more on that in a moment, but let's first dive into our scenario here. 8 00:00:19,560 --> 00:00:22,640 I have here a program called report.py, where 9 00:00:22,640 --> 00:00:26,240 the goal is to write a report on some given spacecraft that's 10 00:00:26,240 --> 00:00:28,130 out there in the universe. 11 00:00:28,130 --> 00:00:31,730 And notice how here I have a function called create_report() that takes 12 00:00:31,730 --> 00:00:36,210 as input something called spacecraft, an argument here called spacecraft. 13 00:00:36,210 --> 00:00:40,370 And my goal is to, well, one, hopefully make sure this report actually has 14 00:00:40,370 --> 00:00:44,750 the information I'm looking for and then return that report for printing, 15 00:00:44,750 --> 00:00:47,480 perhaps up in main() over here. 16 00:00:47,480 --> 00:00:50,300 Now, we said that dictionaries are good for storing 17 00:00:50,300 --> 00:00:52,560 collections of related information. 18 00:00:52,560 --> 00:00:55,680 And I think a spacecraft kind of qualifies in this sense. 19 00:00:55,680 --> 00:01:00,660 A spacecraft has something like a name, maybe a distance from Earth, let's say. 20 00:01:00,660 --> 00:01:05,500 So I think a spacecraft could be good to represent with a dictionary. 21 00:01:05,500 --> 00:01:06,880 Well, let's try this out here. 22 00:01:06,880 --> 00:01:11,650 I'll go head up to main() here, and I will create our very first spacecraft. 23 00:01:11,650 --> 00:01:14,340 I'll call, in this case, just spacecraft. 24 00:01:14,340 --> 00:01:18,150 And I'll set spacecraft equal to, well, a dictionary. 25 00:01:18,150 --> 00:01:22,590 And so we've seen that a dictionary begins with these curly braces. 26 00:01:22,590 --> 00:01:25,330 Whereas a list begins with square brackets, 27 00:01:25,330 --> 00:01:28,620 a dictionary is noted with these curly braces here. 28 00:01:28,620 --> 00:01:32,970 Now, a dictionary, of course, comes with these keys and values. 29 00:01:32,970 --> 00:01:37,740 Now, a key is simply some name, so accessing some particular value 30 00:01:37,740 --> 00:01:40,200 inside of this dictionary. 31 00:01:40,200 --> 00:01:42,330 And maybe the first key we should have here is 32 00:01:42,330 --> 00:01:46,660 some attribute of our spacecraft-- maybe its name, for instance. 33 00:01:46,660 --> 00:01:49,680 So I'll say here one of our keys is "name." 34 00:01:49,680 --> 00:01:53,280 And syntax-wise, what I have to do when I specify a key is this. 35 00:01:53,280 --> 00:01:55,360 I give the name of the key. 36 00:01:55,360 --> 00:01:58,720 In this case, the name of the key literally is "name." 37 00:01:58,720 --> 00:02:04,330 I can then type colon followed by the value I hope for this key to represent. 38 00:02:04,330 --> 00:02:08,160 So in this case, maybe let's talk about Voyager 1, 39 00:02:08,160 --> 00:02:12,150 which is the furthest manmade spacecraft, really, any kind of object, 40 00:02:12,150 --> 00:02:15,000 away from Earth, "Voyager 1" here. 41 00:02:15,000 --> 00:02:18,400 So here our spacecraft is a dictionary. 42 00:02:18,400 --> 00:02:23,230 And we have one key, "name," that is assigned the value "Voyager 1." 43 00:02:23,230 --> 00:02:26,580 But, of course, there are more really properties of Voyager 1 44 00:02:26,580 --> 00:02:28,810 we can actually put inside of this dictionary. 45 00:02:28,810 --> 00:02:31,320 One of them is distance-- 46 00:02:31,320 --> 00:02:32,140 "distance." 47 00:02:32,140 --> 00:02:35,980 And we said Voyager 1 is the furthest manmade object from Earth. 48 00:02:35,980 --> 00:02:41,130 So we'll go ahead and say that this is 163 Astronomical Units away, 49 00:02:41,130 --> 00:02:44,290 which is on the order of, let's say, billions of miles away. 50 00:02:44,290 --> 00:02:47,010 Go ahead and look up what that conversion is on your own. 51 00:02:47,010 --> 00:02:52,860 So here we have Voyager 1, and we also have a distance of 163 AU. 52 00:02:52,860 --> 00:02:55,510 And this together forms our spacecraft. 53 00:02:55,510 --> 00:02:59,970 So if I want to create a report on this spacecraft, I could, as we said before, 54 00:02:59,970 --> 00:03:05,340 maybe print the result of create_report() but then give as input 55 00:03:05,340 --> 00:03:07,150 our spacecraft down below. 56 00:03:07,150 --> 00:03:11,130 And hopefully, if we run python report.py-- 57 00:03:11,130 --> 00:03:12,690 hmm. 58 00:03:12,690 --> 00:03:14,700 Well, we see a report. 59 00:03:14,700 --> 00:03:18,660 I argue that create_report() is returning us some text, 60 00:03:18,660 --> 00:03:20,280 and we're printing it in main(). 61 00:03:20,280 --> 00:03:23,380 But we haven't fixed these TODOs. 62 00:03:23,380 --> 00:03:29,320 So we have here our own dictionary with keys and values that we have specified. 63 00:03:29,320 --> 00:03:33,480 But if we want to access those values, given some key, well, we can do that, 64 00:03:33,480 --> 00:03:35,920 perhaps, in create_report(). 65 00:03:35,920 --> 00:03:38,130 Now, in general, we could assume, let's say, 66 00:03:38,130 --> 00:03:41,520 that create_report() is given some spacecraft that is a dictionary with 67 00:03:41,520 --> 00:03:44,410 these two keys, "name" and "distance." 68 00:03:44,410 --> 00:03:48,460 And the goal here is really to just access the values at those keys. 69 00:03:48,460 --> 00:03:53,110 So I could use here our Python f-string and use curly braces of my own. 70 00:03:53,110 --> 00:03:55,750 These are not curly braces for a dictionary. 71 00:03:55,750 --> 00:03:58,980 These are curly braces for interpolating some value. 72 00:03:58,980 --> 00:04:01,680 I can put in Python code here to get some value 73 00:04:01,680 --> 00:04:06,070 and actually incorporate it into this text called REPORT here. 74 00:04:06,070 --> 00:04:10,710 So for the name of our spacecraft, we know we have a dictionary that's called 75 00:04:10,710 --> 00:04:13,870 spacecraft being passed as input to create_report(). 76 00:04:13,870 --> 00:04:17,760 And we also know, and we can assume in this case that this dictionary will have 77 00:04:17,760 --> 00:04:20,920 a key called "name," all lowercase. 78 00:04:20,920 --> 00:04:24,450 So to access the value at this key, we can use brackets 79 00:04:24,450 --> 00:04:28,960 followed by the actual key of our-- actual name of our key. 80 00:04:28,960 --> 00:04:32,070 In this case, the name of our key literally is "name." 81 00:04:32,070 --> 00:04:36,330 So here, to be clear, what we'll do is go ahead and interpolate 82 00:04:36,330 --> 00:04:38,560 some value inside of this f-string. 83 00:04:38,560 --> 00:04:42,720 And this value will be the value we access by accessing the name-- 84 00:04:42,720 --> 00:04:47,070 the key name-- as part of our spacecraft dictionary here. 85 00:04:47,070 --> 00:04:49,060 Let's do the same thing now for distance. 86 00:04:49,060 --> 00:04:53,700 We can assume that our spacecraft dictionary also has a key called 87 00:04:53,700 --> 00:04:55,480 "distance" just like this. 88 00:04:55,480 --> 00:04:58,170 And now, if we were to rerun this report, 89 00:04:58,170 --> 00:05:02,460 I think we'd see that we get a report back with our name being Voyager 1 90 00:05:02,460 --> 00:05:04,940 and our distance being 163. 91 00:05:04,940 --> 00:05:06,120 But there are no units here. 92 00:05:06,120 --> 00:05:07,953 So let's go ahead and actually add this in-- 93 00:05:07,953 --> 00:05:12,230 163 AU, or Astronomical Units. 94 00:05:12,230 --> 00:05:13,190 All right. 95 00:05:13,190 --> 00:05:15,270 So this is our report. 96 00:05:15,270 --> 00:05:18,270 And you get a sense for how handy these things like dictionaries can be. 97 00:05:18,270 --> 00:05:21,940 We are able here to combine various pieces of information-- 98 00:05:21,940 --> 00:05:23,690 the name of our spacecraft, the distance-- 99 00:05:23,690 --> 00:05:29,600 and then access it very easily using these keys that we have down below. 100 00:05:29,600 --> 00:05:33,420 Now, what might go wrong with dictionaries? 101 00:05:33,420 --> 00:05:36,780 Well, let's say we're not using Voyager 1 anymore. 102 00:05:36,780 --> 00:05:40,940 We're using a new spacecraft, or more specifically, a kind of telescope 103 00:05:40,940 --> 00:05:45,740 called the James Webb Space Telescope, one of the most recent space telescopes 104 00:05:45,740 --> 00:05:49,740 created that can help us see far beyond the reaches of space. 105 00:05:49,740 --> 00:05:54,800 So here I'll make a new dictionary, and I'll set the name to "James Webb Space 106 00:05:54,800 --> 00:05:55,770 Telescope." 107 00:05:55,770 --> 00:06:00,620 But I'm actually not quite sure how far away this telescope is from Earth. 108 00:06:00,620 --> 00:06:03,410 So I'll leave that key empty here. 109 00:06:03,410 --> 00:06:07,620 Well, I can go ahead and try to run python of report.py. 110 00:06:07,620 --> 00:06:11,940 But before I do, maybe think about what might go wrong when I call 111 00:06:11,940 --> 00:06:13,600 create_report(). 112 00:06:13,600 --> 00:06:14,590 Let's see here. 113 00:06:14,590 --> 00:06:15,690 I'll hit Enter. 114 00:06:15,690 --> 00:06:19,300 And I'll actually get what's called a key error down below. 115 00:06:19,300 --> 00:06:22,300 I see key error, colon, 'distance.' 116 00:06:22,300 --> 00:06:27,720 And it seems to me that we tried to access this key named "distance" 117 00:06:27,720 --> 00:06:31,120 in create_report(), but our assumption was not quite correct. 118 00:06:31,120 --> 00:06:35,050 Spacecraft does not have a key called "distance." 119 00:06:35,050 --> 00:06:37,960 So let's go ahead and add that key here. 120 00:06:37,960 --> 00:06:41,250 Well, it turns out that to add a new key to a dictionary, 121 00:06:41,250 --> 00:06:45,130 I don't need to just simply create it as a dictionary is being built. 122 00:06:45,130 --> 00:06:47,140 I could also do this. 123 00:06:47,140 --> 00:06:54,000 I could go ahead and try to do spaceraft, bracket, "distance," 124 00:06:54,000 --> 00:06:57,810 much like the syntax down below, to access this key, 125 00:06:57,810 --> 00:07:00,190 but then supply some value. 126 00:07:00,190 --> 00:07:04,600 In this case, I could say that it is, I think-- let me just check here-- 127 00:07:04,600 --> 00:07:08,320 it is 0.01 AU away from Earth. 128 00:07:08,320 --> 00:07:11,370 So actually, interestingly, the James Webb Space Telescope kind of 129 00:07:11,370 --> 00:07:14,880 orbits the sun in a kind of constant distance around 130 00:07:14,880 --> 00:07:17,740 from Earth-- so about 0.01 AU here. 131 00:07:17,740 --> 00:07:19,540 Let's go ahead and run this report again. 132 00:07:19,540 --> 00:07:22,170 I'll say python of report.py, and now we'll 133 00:07:22,170 --> 00:07:26,790 see that our report has been fixed for us down below. 134 00:07:26,790 --> 00:07:30,510 So we've seen here various ways to add keys 135 00:07:30,510 --> 00:07:33,570 to dictionaries, various ways to access those keys 136 00:07:33,570 --> 00:07:35,680 and to create our own dictionary. 137 00:07:35,680 --> 00:07:40,750 Let's consider more ways to actually access these values for a given key. 138 00:07:40,750 --> 00:07:44,550 Let's go back a little bit and assume we actually don't have this value, 0.01. 139 00:07:44,550 --> 00:07:47,140 We have just here the key "name." 140 00:07:47,140 --> 00:07:52,290 What could we do to perhaps, maybe, anticipate this error and make 141 00:07:52,290 --> 00:07:56,890 create_report() a little more flexible with the kinds of input it might get? 142 00:07:56,890 --> 00:07:59,490 Well, it turns out that to access some key, 143 00:07:59,490 --> 00:08:02,260 we don't need to always use this bracket notation. 144 00:08:02,260 --> 00:08:05,640 In fact, there is some other method, one called get, 145 00:08:05,640 --> 00:08:08,640 we can use to try to access some key. 146 00:08:08,640 --> 00:08:10,560 And if that key doesn't exist, we'll actually 147 00:08:10,560 --> 00:08:13,840 get some other value we specify instead. 148 00:08:13,840 --> 00:08:19,920 So here, perhaps in "distance," I could, instead of using bracket "distance" 149 00:08:19,920 --> 00:08:22,350 here, I could use dot get. 150 00:08:22,350 --> 00:08:25,845 So the name of my dictionary, spacecraft, dot get, 151 00:08:25,845 --> 00:08:32,340 followed by the key I hope to access in my dictionary, in this case, "distance." 152 00:08:32,340 --> 00:08:37,679 I could, if I wanted to, supply another value that will actually be returned 153 00:08:37,679 --> 00:08:42,220 if this "distance" key is not part of my dictionary. 154 00:08:42,220 --> 00:08:45,760 And we saw earlier that "distance" is not part of this dictionary. 155 00:08:45,760 --> 00:08:49,780 So what we should hopefully see instead is "unknown." 156 00:08:49,780 --> 00:08:53,760 Why don't I go ahead and try python of report.py, hit Enter, 157 00:08:53,760 --> 00:08:58,860 and now we'll actually see that get returned to us "Unknown" 158 00:08:58,860 --> 00:09:02,880 instead of the actual value for this nonexistent key. 159 00:09:02,880 --> 00:09:06,250 This avoids the key error we saw earlier as well. 160 00:09:06,250 --> 00:09:07,270 So let's try this here. 161 00:09:07,270 --> 00:09:12,550 I'll do spacecraft.get, and I'll try to get the name of the spacecraft. 162 00:09:12,550 --> 00:09:16,140 Otherwise, we can go ahead and return "Unknown" as well. 163 00:09:16,140 --> 00:09:19,480 So in general, you'll find that most of the time, 164 00:09:19,480 --> 00:09:22,540 you can use that same bracket notation we saw earlier. 165 00:09:22,540 --> 00:09:25,530 But if you aren't sure if some key will exist, 166 00:09:25,530 --> 00:09:30,040 you could use get to try to get that key but return some other value 167 00:09:30,040 --> 00:09:33,720 if that key in fact, does not exist inside 168 00:09:33,720 --> 00:09:37,290 of your particular dictionary in this case. 169 00:09:37,290 --> 00:09:44,290 So I believe this gives us an example of how we can go ahead and get our keys. 170 00:09:44,290 --> 00:09:48,990 Let's now explore other ways to add keys beyond just the same syntax we 171 00:09:48,990 --> 00:09:50,830 saw earlier as well. 172 00:09:50,830 --> 00:09:56,500 So recall we could go ahead and add in a key by creating a dictionary up here. 173 00:09:56,500 --> 00:09:58,140 I could say "distance"-- 174 00:09:58,140 --> 00:10:00,600 "distance," colon, et cetera, here. 175 00:10:00,600 --> 00:10:03,180 I could even do some syntax like this-- 176 00:10:03,180 --> 00:10:09,480 spacecraft "distance" equals, let's say, 0.01. 177 00:10:09,480 --> 00:10:11,850 These are two ways to add keys to a dictionary. 178 00:10:11,850 --> 00:10:15,870 It turns out there's also one more to be familiar with as well. 179 00:10:15,870 --> 00:10:20,390 If I wanted to add not just one key at a time to my dictionary, 180 00:10:20,390 --> 00:10:24,910 but maybe multiple at once, I could use a method called update. 181 00:10:24,910 --> 00:10:28,370 So I could type the name of my dictionary followed by a dot 182 00:10:28,370 --> 00:10:30,640 and then update, just like this. 183 00:10:30,640 --> 00:10:35,910 And update takes as an argument, as an input here, another dictionary. 184 00:10:35,910 --> 00:10:39,110 But it will really take that dictionary's keys and values 185 00:10:39,110 --> 00:10:41,990 and just add them to the dictionary I started 186 00:10:41,990 --> 00:10:43,920 with here-- in this case, spacecraft. 187 00:10:43,920 --> 00:10:47,750 So it will update spacecraft with all of the keys and values 188 00:10:47,750 --> 00:10:50,840 that I provide as input to update. 189 00:10:50,840 --> 00:10:54,680 Now, I could go ahead and specify here in another dictionary 190 00:10:54,680 --> 00:11:00,030 maybe the distance, which we said before was 0.01 AU. 191 00:11:00,030 --> 00:11:04,920 And then we can maybe specify another attribute, maybe the orbit here. 192 00:11:04,920 --> 00:11:11,780 As we said before, the James Webb Space Telescope orbits the sun in this case. 193 00:11:11,780 --> 00:11:16,440 So here, I'm trying to add now two new keys, one called "distance" 194 00:11:16,440 --> 00:11:20,490 and one called "orbit," each with their own values. 195 00:11:20,490 --> 00:11:24,530 I could also go down below here, and I could add in some new line of my report. 196 00:11:24,530 --> 00:11:30,240 Maybe I'll say "Orbit," and that is equal to maybe spacecraft.get the key 197 00:11:30,240 --> 00:11:31,510 called "orbit." 198 00:11:31,510 --> 00:11:36,600 Otherwise, we'll return "Unknown" if that key perhaps doesn't exist down 199 00:11:36,600 --> 00:11:38,160 below. 200 00:11:38,160 --> 00:11:38,980 All right. 201 00:11:38,980 --> 00:11:44,700 So here we've tried to add here two new keys at once to our dictionary up above. 202 00:11:44,700 --> 00:11:47,670 Let's go ahead and run python of report.py. 203 00:11:47,670 --> 00:11:52,200 And now we'll see that those keys are in fact, part of our dictionary thanks 204 00:11:52,200 --> 00:11:54,510 to update as well. 205 00:11:54,510 --> 00:11:58,650 So now we've seen how to add keys in various ways, 206 00:11:58,650 --> 00:12:00,880 how to access keys in various ways. 207 00:12:00,880 --> 00:12:05,410 But let's see some other utility features of dictionaries as well. 208 00:12:05,410 --> 00:12:07,990 I'll actually go ahead and remove what I have here. 209 00:12:07,990 --> 00:12:14,770 And why don't we try to make a new program, one called distance.py? 210 00:12:14,770 --> 00:12:18,870 Now, in distances.py, we're going to treat dictionaries a little bit 211 00:12:18,870 --> 00:12:19,890 differently. 212 00:12:19,890 --> 00:12:24,300 I'm going to make a dictionary where I have the names of my spacecraft in one 213 00:12:24,300 --> 00:12:28,890 column, if you will, and the distances of those spacecraft in another column, 214 00:12:28,890 --> 00:12:30,280 if you will, in my dictionary. 215 00:12:30,280 --> 00:12:34,020 So, for instance, I can make a dictionary called distances 216 00:12:34,020 --> 00:12:36,370 and open up some curly braces here. 217 00:12:36,370 --> 00:12:40,920 And as the keys in this dictionary, I'll go ahead and have the names 218 00:12:40,920 --> 00:12:44,570 of spacecraft, like "Voyager 1," for instance. 219 00:12:44,570 --> 00:12:48,390 Now, the value of this particular key will be the distance 220 00:12:48,390 --> 00:12:55,380 that Voyager 1 is away from Earth, which happens to be 163 AU, as we saw before. 221 00:12:55,380 --> 00:12:59,940 Now, I can go ahead and add other keys to my dictionary, other spacecraft 222 00:12:59,940 --> 00:13:01,510 and their distances. 223 00:13:01,510 --> 00:13:07,260 I can also go ahead and add "Voyager 2," which happens to be 136 AU away from 224 00:13:07,260 --> 00:13:07,950 Earth. 225 00:13:07,950 --> 00:13:14,640 I could go ahead and add "Pioneer 10," which is about 80 AU away, 226 00:13:14,640 --> 00:13:20,550 "New Horizons," another probe, which is about 58 AU away, and, finally, 227 00:13:20,550 --> 00:13:25,290 "Pioneer 11," which is about 44 AU away. 228 00:13:25,290 --> 00:13:29,820 So notice how here, my dictionary is representing multiple spacecraft, 229 00:13:29,820 --> 00:13:32,700 but I'm really defining the relationship between my spacecraft 230 00:13:32,700 --> 00:13:35,820 names and their distances as well. 231 00:13:35,820 --> 00:13:38,190 So with my information represented like this, 232 00:13:38,190 --> 00:13:41,130 I could try to maybe print out each of their names 233 00:13:41,130 --> 00:13:45,450 and their distances using some key functionalities of dictionaries here. 234 00:13:45,450 --> 00:13:48,420 I'll go ahead and define myself a function called main(), 235 00:13:48,420 --> 00:13:50,190 and I'll leave that empty for now. 236 00:13:50,190 --> 00:13:52,630 I'll go down below and make sure I call main(). 237 00:13:52,630 --> 00:13:58,920 And now, within main(), my goal is to, for each spacecraft I have inside 238 00:13:58,920 --> 00:14:03,480 of this dictionary, print out its name and the distance away from Earth. 239 00:14:03,480 --> 00:14:07,650 So if I wanted to do something for each, let's say, anything I have, 240 00:14:07,650 --> 00:14:10,270 a good idea is to use a for loop. 241 00:14:10,270 --> 00:14:16,188 In this case, I want to do something for each key I have in my dictionary. 242 00:14:16,188 --> 00:14:18,480 Well, it turns out that dictionaries come with a method 243 00:14:18,480 --> 00:14:23,440 called keys that returns to me all the keys in my dictionary. 244 00:14:23,440 --> 00:14:29,460 So I could type for name in distances.keys(). 245 00:14:29,460 --> 00:14:34,920 And, again, dot keys, this method here, that will return to me all of the keys 246 00:14:34,920 --> 00:14:40,410 in my dictionary-- so first "Voyager 1," then "Voyager 2," then "Pioneer 10," 247 00:14:40,410 --> 00:14:42,460 "New Horizons," "Pioneer 11." 248 00:14:42,460 --> 00:14:46,500 These will all be part of the return value of dot keys 249 00:14:46,500 --> 00:14:49,300 when called on the distances dictionary. 250 00:14:49,300 --> 00:14:53,320 And now I'm saying for name in that list of keys. 251 00:14:53,320 --> 00:14:59,260 So first name will be equal to "Voyager 1," then "Voyager 2," then "Pioneer 10," 252 00:14:59,260 --> 00:15:04,230 as my for loop continues in the code indented here on line 12. 253 00:15:04,230 --> 00:15:09,420 So now that I have name, which will first be "Voyager 1," "Voyager 2," 254 00:15:09,420 --> 00:15:11,940 I might also want to access, in this case, 255 00:15:11,940 --> 00:15:15,160 the distances of those probes from Earth. 256 00:15:15,160 --> 00:15:20,070 And to do that, I could actually maybe plug in my key to my dictionary 257 00:15:20,070 --> 00:15:22,303 and get back the value I'm looking for. 258 00:15:22,303 --> 00:15:23,470 Let's demonstrate this here. 259 00:15:23,470 --> 00:15:26,100 I'll type print(), and I'll use an f-string. 260 00:15:26,100 --> 00:15:28,230 And I'll then say "name"-- 261 00:15:28,230 --> 00:15:31,620 referring to the name of my spacecraft-- "is" 262 00:15:31,620 --> 00:15:34,920 and then some distance away from Earth. 263 00:15:34,920 --> 00:15:40,060 But, again, we have here "name," which is in fact, a key of our dictionary. 264 00:15:40,060 --> 00:15:43,800 So I could type "distances," bracket, "name," 265 00:15:43,800 --> 00:15:48,400 to access the value for each of these various keys I'm getting. 266 00:15:48,400 --> 00:15:51,510 For instance, when name is equal to "Voyager 1," 267 00:15:51,510 --> 00:15:54,370 I'll get back 163 right here. 268 00:15:54,370 --> 00:16:00,010 When name is equal to "Voyager 2," well, I'll get back 136 right down here. 269 00:16:00,010 --> 00:16:01,860 So I could complete the sentence by saying 270 00:16:01,860 --> 00:16:07,770 name is some distance in AU from Earth, just like that. 271 00:16:07,770 --> 00:16:09,490 Let's go ahead and try running this. 272 00:16:09,490 --> 00:16:13,470 I'll say python of distances.py. 273 00:16:13,470 --> 00:16:17,770 And we'll see here some other reports on our spacecraft. 274 00:16:17,770 --> 00:16:21,310 Voyager 1 is 163 AU from Earth. 275 00:16:21,310 --> 00:16:24,820 Voyager 2 is 136 AU from Earth. 276 00:16:24,820 --> 00:16:28,740 So when you want to do some kind of code, like this on line 12, 277 00:16:28,740 --> 00:16:32,430 for each key in your dictionary, you could use dot keys 278 00:16:32,430 --> 00:16:35,340 to access each of those keys and loop over them, perhaps, 279 00:16:35,340 --> 00:16:37,890 with something like a for loop. 280 00:16:37,890 --> 00:16:39,970 One more to be familiar with here-- 281 00:16:39,970 --> 00:16:44,500 you can also loop over, let's say, the values in our dictionaries. 282 00:16:44,500 --> 00:16:49,570 So just like we have keys, we also have values as well, dot values. 283 00:16:49,570 --> 00:16:54,420 And let's try maybe converting these AU to meters instead, 284 00:16:54,420 --> 00:16:56,710 a much more friendly unit of measurement. 285 00:16:56,710 --> 00:17:00,520 So I'll go ahead and replace this code down below here. 286 00:17:00,520 --> 00:17:03,520 And why don't I actually delete this for loop here 287 00:17:03,520 --> 00:17:08,290 and first give myself a function to convert AU-- 288 00:17:08,290 --> 00:17:12,589 to convert AU into, in this case, meters? 289 00:17:12,589 --> 00:17:17,619 And it turns out that the number of meters corresponding to 1 290 00:17:17,619 --> 00:17:20,468 AU is this big number here. 291 00:17:20,468 --> 00:17:22,510 I'm not even sure how to pronounce it, but I will 292 00:17:22,510 --> 00:17:24,339 make sure to type it in correctly-- 293 00:17:24,339 --> 00:17:28,450 597870700. 294 00:17:28,450 --> 00:17:30,310 And I believe this is correct-- 295 00:17:30,310 --> 00:17:35,020 870, 597, 149. 296 00:17:35,020 --> 00:17:35,540 Yes. 297 00:17:35,540 --> 00:17:37,990 This really big number that I don't know how to pronounce 298 00:17:37,990 --> 00:17:41,900 is the number of meters in 1 given AU. 299 00:17:41,900 --> 00:17:46,510 So if we wanted to perhaps loop over all of our values 300 00:17:46,510 --> 00:17:51,940 in the dictionary-- so first 163, then 136, then 80 and so on, 301 00:17:51,940 --> 00:17:54,160 we could use a for loop. 302 00:17:54,160 --> 00:17:59,770 I could say for, perhaps, distance in distances.values. 303 00:17:59,770 --> 00:18:07,060 And then I could perhaps print using an f-string the distance in AU 304 00:18:07,060 --> 00:18:13,120 is the converted distance in meters. 305 00:18:13,120 --> 00:18:18,610 So to be clear here, a function called convert() that takes an amount of AU 306 00:18:18,610 --> 00:18:20,420 and converts it to meters-- 307 00:18:20,420 --> 00:18:27,040 then, in main(), we go through each value in our dictionary's values, 308 00:18:27,040 --> 00:18:31,300 and then we'll go ahead and convert that distance to meters and also print out 309 00:18:31,300 --> 00:18:33,130 the distance in AU. 310 00:18:33,130 --> 00:18:37,450 So I'll go ahead here and I'll run python of distances.py. 311 00:18:37,450 --> 00:18:42,070 And now we'll hopefully see down below all of these distances in AU 312 00:18:42,070 --> 00:18:44,590 now in meters. 313 00:18:44,590 --> 00:18:47,110 So we've seen a lot about dictionaries. 314 00:18:47,110 --> 00:18:50,320 We've seen, back in report.py, how to create them, 315 00:18:50,320 --> 00:18:53,110 how to supply keys and values. 316 00:18:53,110 --> 00:18:55,750 We've seen how to update our dictionaries, 317 00:18:55,750 --> 00:18:58,810 whether using the update method or by using bracket 318 00:18:58,810 --> 00:19:01,030 notation we saw a little earlier too. 319 00:19:01,030 --> 00:19:05,300 We've seen how to access those values given some key. 320 00:19:05,300 --> 00:19:09,280 We've seen the get method down below as well as the more simple bracket 321 00:19:09,280 --> 00:19:10,750 notation, too. 322 00:19:10,750 --> 00:19:16,600 And over in distances.py, we've seen how to loop over the keys and the values 323 00:19:16,600 --> 00:19:18,500 in our dictionaries. 324 00:19:18,500 --> 00:19:20,690 That's it here for our short on dictionaries. 325 00:19:20,690 --> 00:19:23,160 We'll see you next time. 326 00:19:23,160 --> 00:19:27,000