1 00:00:07,880 --> 00:00:10,890 Welcome back. In this lesson, 2 00:00:10,890 --> 00:00:15,495 we're going to learn about some operations that we can use to modify dictionaries. 3 00:00:15,495 --> 00:00:17,220 So here on line 1, 4 00:00:17,220 --> 00:00:18,540 we create a dictionary. 5 00:00:18,540 --> 00:00:21,510 In this dictionary, we have four key-value pairs. 6 00:00:21,510 --> 00:00:25,965 So the key pears is associated with the integer 217. 7 00:00:25,965 --> 00:00:31,980 The key and the string apples is associated with 430, and so on. 8 00:00:31,980 --> 00:00:38,445 So, overall we have four key-value pairs and that's all created on line 1. 9 00:00:38,445 --> 00:00:42,470 Now, on line 2, we do an operation called del. 10 00:00:42,470 --> 00:00:44,810 So, del is short for Delete, 11 00:00:44,810 --> 00:00:48,590 and Delete deletes a key-value pair from our dictionary. 12 00:00:48,590 --> 00:00:53,265 So, when we say "del" and then we say the name of the dictionary, 13 00:00:53,265 --> 00:00:56,520 and then in square brackets the key, 14 00:00:56,520 --> 00:00:58,875 whose key-value pair we want to delete, 15 00:00:58,875 --> 00:01:02,725 that's going to get rid of that key-value pair from our dictionary. 16 00:01:02,725 --> 00:01:06,705 So, here we're saying del inventory sub pears. 17 00:01:06,705 --> 00:01:13,430 So, here our key-value pair pears is associated with the value 217. 18 00:01:13,430 --> 00:01:17,695 Line 3 says get rid of this key-value pair. 19 00:01:17,695 --> 00:01:20,265 So, when I actually run line 3, 20 00:01:20,265 --> 00:01:26,670 you'll notice the number of key-value pairs will go from four to three, 21 00:01:29,120 --> 00:01:33,050 if instead we created the same dictionary. 22 00:01:33,050 --> 00:01:36,980 So again, we're back to having four key-value pairs. 23 00:01:36,980 --> 00:01:41,390 Now, in line 3, rather than deleting the value associated with pears, 24 00:01:41,390 --> 00:01:43,630 then we're going to set it to zero. 25 00:01:43,630 --> 00:01:47,389 So, if you remember from when we first introduced dictionaries, 26 00:01:47,389 --> 00:01:52,535 we can set a key-value pair by saying the name of the dictionary, sub, 27 00:01:52,535 --> 00:01:55,570 and then whatever key we want to set, 28 00:01:55,570 --> 00:01:59,390 equals whatever value we want it to be associated with. 29 00:01:59,390 --> 00:02:03,725 But the difference here is that by the time we run line 3, 30 00:02:03,725 --> 00:02:07,235 we'll already have a key-value pair that associates 31 00:02:07,235 --> 00:02:11,595 the key pears with the different value 217. 32 00:02:11,595 --> 00:02:16,155 So, when we say inventory sub pears now equals zero, 33 00:02:16,155 --> 00:02:20,510 that's going to update the value associated with the key pairs. 34 00:02:20,510 --> 00:02:26,980 So, in other words, line 3 is going to say pears is not 217 anymore, 35 00:02:26,980 --> 00:02:30,590 it's now associated with the value zero. 36 00:02:30,590 --> 00:02:34,325 So here, if we now run line 3, 37 00:02:34,325 --> 00:02:38,275 then you'll see the value of pears is now zero. 38 00:02:38,275 --> 00:02:41,800 So, let's reuse that dictionary in another example. 39 00:02:41,800 --> 00:02:45,220 So on line 1, we create the same dictionary that 40 00:02:45,220 --> 00:02:48,985 has four key-value pairs that we saw from the previous examples. 41 00:02:48,985 --> 00:02:50,910 Now, on line 2, 42 00:02:50,910 --> 00:02:52,410 we're setting that dictionary, 43 00:02:52,410 --> 00:02:58,425 so inventory sub bananas to inventory sub bananas plus 200. 44 00:02:58,425 --> 00:03:00,105 Now, when we look at this, 45 00:03:00,105 --> 00:03:02,850 this expression is a little confusing because here we're 46 00:03:02,850 --> 00:03:06,600 repeating this expression inventory sub bananas. 47 00:03:06,600 --> 00:03:09,975 But remember that when we do in assignment, 48 00:03:09,975 --> 00:03:12,325 then what Python does is it first 49 00:03:12,325 --> 00:03:15,715 evaluates the value that we're going to be assigning to. 50 00:03:15,715 --> 00:03:22,620 So, the first thing Python does on line 2 is it asks what's the value of this expression, 51 00:03:22,620 --> 00:03:25,695 inventory sub bananas plus 200? 52 00:03:25,695 --> 00:03:28,375 After it computes this value, 53 00:03:28,375 --> 00:03:33,925 then it's going to assign whatever that value is to inventory sub bananas. 54 00:03:33,925 --> 00:03:36,800 So, in other words, to figure out what this is going to do, 55 00:03:36,800 --> 00:03:41,000 we first have to ask what's the value of this expression? 56 00:03:41,000 --> 00:03:45,425 So, to figure out the value of this overall expression, let's break it down. 57 00:03:45,425 --> 00:03:48,910 So, we add inventory sub bananas to 200, 58 00:03:48,910 --> 00:03:52,370 so we ask what's the value of inventory sub bananas? 59 00:03:52,370 --> 00:03:55,685 To figure that out, we look up this dictionary and see that 60 00:03:55,685 --> 00:03:59,165 bananas is associated with 312. 61 00:03:59,165 --> 00:04:02,800 So this value is 312. 62 00:04:02,800 --> 00:04:08,800 Then, we add 200 to that to get 512. 63 00:04:09,770 --> 00:04:14,120 Then, Python takes this integer 512 and it 64 00:04:14,120 --> 00:04:18,095 makes it the new value associated with the key bananas. 65 00:04:18,095 --> 00:04:23,780 So, this is going to now be 512 when we run line 2. 66 00:04:23,780 --> 00:04:30,500 So, let's run line 2, and we see bananas is now 512. 67 00:04:30,500 --> 00:04:36,980 Now, if you remember the "len" function from strings or lists or tuples, 68 00:04:36,980 --> 00:04:40,660 remember that len gives you the number of items in a collection. 69 00:04:40,660 --> 00:04:42,910 So, in the case of strings, 70 00:04:42,910 --> 00:04:45,785 len gives us the number of characters in that string. 71 00:04:45,785 --> 00:04:48,115 Len also works with dictionaries. 72 00:04:48,115 --> 00:04:51,110 So, if we pass in the dictionary inventory, 73 00:04:51,110 --> 00:04:52,930 then when we call len on it, 74 00:04:52,930 --> 00:05:00,310 the value of this expression gives us the number of key-value pairs in this dictionary. 75 00:05:00,310 --> 00:05:03,845 So, the value of this expression is going to be one, 76 00:05:03,845 --> 00:05:07,790 two, three, four, because there are four key-value pairs. 77 00:05:07,790 --> 00:05:10,790 So, we'll see you that "numItems" in 78 00:05:10,790 --> 00:05:16,695 our frame right here is going to be assigned to the value, the integer four. 79 00:05:16,695 --> 00:05:23,790 So, let's run line 4 and we see that numItems is now four. 80 00:05:24,200 --> 00:05:27,210 So, let's do some more questions. 81 00:05:27,210 --> 00:05:31,575 In this question we ask what is printed by the following statements? 82 00:05:31,575 --> 00:05:35,420 So, here we create a dictionary with three key-value pairs. 83 00:05:35,420 --> 00:05:40,800 Then, we say my dictionary sub mouse equals my dictionary sub cat, 84 00:05:40,800 --> 00:05:43,185 plus my dictionary sub dog. 85 00:05:43,185 --> 00:05:45,750 So, the value of this expression, 86 00:05:45,750 --> 00:05:49,140 my dictionary sub cat, is 12. 87 00:05:49,140 --> 00:05:50,985 The value of this expression, 88 00:05:50,985 --> 00:05:54,419 my dictionary sub dog, is six, 89 00:05:54,419 --> 00:05:57,605 meaning that the value of this overall expression, 90 00:05:57,605 --> 00:06:03,720 my dictionary sub cat plus my dictionary sub dog is 12 plus 6 or 18. 91 00:06:04,630 --> 00:06:12,005 So, then we assign the value 18 to being associated with the key mouse. 92 00:06:12,005 --> 00:06:15,050 So, by the time we print out my dictionary sub mouse, 93 00:06:15,050 --> 00:06:16,910 we're going to print out 18. 94 00:06:16,910 --> 00:06:21,880 So our answer is C. So, 95 00:06:21,880 --> 00:06:26,750 this question asks us to update the value for Phelps in the dictionary swimmers, 96 00:06:26,750 --> 00:06:32,240 to include his medals from the Rio Olympics by adding five to the current value. 97 00:06:32,240 --> 00:06:35,440 So, Phelps will now have 28 total medals, 98 00:06:35,440 --> 00:06:39,075 and it asks us do not rewrite the dictionary. 99 00:06:39,075 --> 00:06:41,095 So, here on line 2, 100 00:06:41,095 --> 00:06:46,045 we assigned swimmers to be a dictionary that has one, two, three, 101 00:06:46,045 --> 00:06:49,440 four, five, six key-value pairs, 102 00:06:49,440 --> 00:06:54,785 but we want to update the value associated with the key Phelps. 103 00:06:54,785 --> 00:07:00,175 In the way that we want to update it is by adding five to the current value. 104 00:07:00,175 --> 00:07:04,430 So, the way that we're going to do that is we're going to say, 105 00:07:04,430 --> 00:07:13,110 swimmers sub Phelps is now its previous value. 106 00:07:13,110 --> 00:07:22,155 So swimmers sub Phelps equals swimmers sub Phelps plus five. 107 00:07:22,155 --> 00:07:24,345 So, whatever it started out with, 108 00:07:24,345 --> 00:07:27,820 it's now going to be that, plus five. 109 00:07:29,420 --> 00:07:33,310 That's all for now. Until next time.