1 00:00:00,660 --> 00:00:01,680 -: Welcome back. 2 00:00:01,680 --> 00:00:03,690 The methods we're gonna talk about 3 00:00:03,690 --> 00:00:06,600 when it comes to sets are right here. 4 00:00:06,600 --> 00:00:07,530 It looks like a lot, 5 00:00:07,530 --> 00:00:08,880 but they're quite similar, 6 00:00:08,880 --> 00:00:10,500 so hang in there. 7 00:00:10,500 --> 00:00:13,140 I have two sets for us, 8 00:00:13,140 --> 00:00:16,470 our my set, which is one through five, 9 00:00:16,470 --> 00:00:19,473 and then your set, which is four through ten. 10 00:00:20,430 --> 00:00:21,990 Now let's go one by one 11 00:00:21,990 --> 00:00:24,600 and see what these methods do. 12 00:00:24,600 --> 00:00:27,420 And if you've ever seen venn diagrams, 13 00:00:27,420 --> 00:00:30,180 in your school for example, 14 00:00:30,180 --> 00:00:32,430 two circles that are overlapping each other, 15 00:00:32,430 --> 00:00:35,730 what we're about to do is quite similar to this. 16 00:00:35,730 --> 00:00:38,160 The first one is difference, 17 00:00:38,160 --> 00:00:41,760 and sets are very useful when having two sets, 18 00:00:41,760 --> 00:00:44,010 and comparing them to each other. 19 00:00:44,010 --> 00:00:45,810 So for example, in here, 20 00:00:45,810 --> 00:00:49,650 difference simply says my set dot difference, 21 00:00:49,650 --> 00:00:52,770 and we give it another set, such as your set. 22 00:00:52,770 --> 00:00:54,870 If I print this, 23 00:00:54,870 --> 00:00:55,800 right here, 24 00:00:55,800 --> 00:00:57,033 and I click run, 25 00:00:58,560 --> 00:01:01,320 I get 1, 2, 3. 26 00:01:01,320 --> 00:01:03,450 It's going to to find the difference 27 00:01:03,450 --> 00:01:07,350 of the first my set, with your set. 28 00:01:07,350 --> 00:01:09,570 So any duplicates between the two, 29 00:01:09,570 --> 00:01:11,193 which is four and five, 30 00:01:12,690 --> 00:01:15,390 gets ignored, and it only shows the difference. 31 00:01:15,390 --> 00:01:16,830 That is 1, 2, 3. 32 00:01:16,830 --> 00:01:18,270 What's different between my set 33 00:01:18,270 --> 00:01:21,798 and your set, when viewed from my set? 34 00:01:21,798 --> 00:01:24,270 Well, your set doesn't have 1, 2, 3. 35 00:01:24,270 --> 00:01:25,620 What about the next one? 36 00:01:25,620 --> 00:01:26,820 Discard. 37 00:01:26,820 --> 00:01:31,293 Again, if we do my set dot discard, 38 00:01:32,220 --> 00:01:34,770 it says it removes an element from a set 39 00:01:34,770 --> 00:01:36,453 if it is a member. 40 00:01:37,320 --> 00:01:42,320 So here, we can say discard five, 41 00:01:42,510 --> 00:01:43,983 and if I do print, 42 00:01:45,870 --> 00:01:47,010 my set, 43 00:01:47,010 --> 00:01:47,943 and I click run, 44 00:01:50,220 --> 00:01:51,510 I get none. 45 00:01:51,510 --> 00:01:53,100 But if I print here, 46 00:01:53,100 --> 00:01:55,110 my set, 47 00:01:55,110 --> 00:01:56,283 and I click run, 48 00:01:58,170 --> 00:02:00,540 I get 1, 2, 3, 4, 49 00:02:00,540 --> 00:02:03,270 because we've just discarded five. 50 00:02:03,270 --> 00:02:05,700 It modifies our set. 51 00:02:05,700 --> 00:02:07,740 Just to keep our working space clean, 52 00:02:07,740 --> 00:02:09,453 I'm going to comment these out. 53 00:02:11,070 --> 00:02:13,713 Next is difference update. 54 00:02:14,910 --> 00:02:17,160 So I'm going to say, my set, 55 00:02:17,160 --> 00:02:18,240 and then once again, 56 00:02:18,240 --> 00:02:20,140 let's see what difference update does. 57 00:02:21,180 --> 00:02:25,620 Remove all elements of another set from this set. 58 00:02:25,620 --> 00:02:29,133 So, if I do, your set here, 59 00:02:30,060 --> 00:02:32,910 and once again, I do print, 60 00:02:32,910 --> 00:02:33,903 and I click run, 61 00:02:36,360 --> 00:02:39,720 again, none, because it just modifies my set. 62 00:02:39,720 --> 00:02:44,020 So remember, we just have to have our print, my set, 63 00:02:47,940 --> 00:02:49,950 and now, this my set, 64 00:02:49,950 --> 00:02:52,500 is updated with just one, two and three, 65 00:02:52,500 --> 00:02:54,370 because we're updating it 66 00:02:55,230 --> 00:02:57,480 so that the differences are removed. 67 00:02:57,480 --> 00:02:59,460 So four and five are removed, 68 00:02:59,460 --> 00:03:00,900 unlike difference, 69 00:03:00,900 --> 00:03:04,503 where it just told you the difference without changing. 70 00:03:05,430 --> 00:03:07,860 Again, if we go back here, 71 00:03:07,860 --> 00:03:09,933 and simply do, my set, 72 00:03:11,518 --> 00:03:12,663 and I click run, 73 00:03:14,550 --> 00:03:16,620 you see that, my set here, 74 00:03:16,620 --> 00:03:19,143 was not modified, right here, 75 00:03:20,040 --> 00:03:22,560 but then, my set bottom, was modified, 76 00:03:22,560 --> 00:03:24,093 because of difference update. 77 00:03:25,800 --> 00:03:27,750 Alright, a few more to go. 78 00:03:27,750 --> 00:03:29,490 Intersection, 79 00:03:29,490 --> 00:03:32,700 and this one, once again, my set. 80 00:03:32,700 --> 00:03:33,930 And in here, once again, 81 00:03:33,930 --> 00:03:35,253 let's read what it does. 82 00:03:36,240 --> 00:03:39,960 It just has intersection, with something else. 83 00:03:39,960 --> 00:03:41,160 Well, let's find out. 84 00:03:41,160 --> 00:03:43,020 If I do your set here, 85 00:03:43,020 --> 00:03:46,593 and I do print, and I run this, 86 00:03:47,610 --> 00:03:48,930 it's going to give me four, five, 87 00:03:48,930 --> 00:03:51,750 because that's the intersection, 88 00:03:51,750 --> 00:03:53,430 the two common things, 89 00:03:53,430 --> 00:03:55,323 that my set and your set has. 90 00:03:56,310 --> 00:03:58,593 What about isdisjoint? 91 00:03:59,970 --> 00:04:04,383 Well, if I do my set and then your set, 92 00:04:05,460 --> 00:04:08,640 I feel like I'm saying those words over and over. 93 00:04:08,640 --> 00:04:10,390 Hopefully, that's not too annoying. 94 00:04:11,370 --> 00:04:15,120 And then here, if, let's comment this out. 95 00:04:15,120 --> 00:04:18,660 We run this, I get false. 96 00:04:18,660 --> 00:04:20,070 Isdisjoint simply says, 97 00:04:20,070 --> 00:04:22,680 hey, are those two circles overlapping, 98 00:04:22,680 --> 00:04:25,410 do they have anything in common? 99 00:04:25,410 --> 00:04:26,790 If for example, 100 00:04:26,790 --> 00:04:28,200 my set didn't have four, 101 00:04:28,200 --> 00:04:29,370 didn't have four and five, 102 00:04:29,370 --> 00:04:32,760 and I click run, I get true, 103 00:04:32,760 --> 00:04:34,380 because isdisjointed, 104 00:04:34,380 --> 00:04:36,573 means these sets have nothing in common. 105 00:04:38,250 --> 00:04:39,783 Awesome, just a few more. 106 00:04:41,250 --> 00:04:44,010 The next ones we're gonna see is, 107 00:04:44,010 --> 00:04:45,600 is issubset, 108 00:04:45,600 --> 00:04:47,523 and issuperset. 109 00:04:48,660 --> 00:04:50,490 But actually before we do that, 110 00:04:50,490 --> 00:04:51,753 let's do union. 111 00:04:52,680 --> 00:04:54,540 Let's see what this does. 112 00:04:54,540 --> 00:04:58,353 Union will say my set, 113 00:05:00,960 --> 00:05:02,340 and in here, 114 00:05:02,340 --> 00:05:05,910 we want to add the other set, your set. 115 00:05:05,910 --> 00:05:07,323 If we print this, 116 00:05:08,760 --> 00:05:09,753 and click run, 117 00:05:12,150 --> 00:05:17,130 I get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 118 00:05:17,130 --> 00:05:18,630 What did that just do? 119 00:05:18,630 --> 00:05:23,630 Well, union just united these sets together, 120 00:05:23,790 --> 00:05:26,250 but removed any duplicates. 121 00:05:26,250 --> 00:05:27,543 That's kind of nice. 122 00:05:28,770 --> 00:05:29,670 And as you can see, 123 00:05:29,670 --> 00:05:31,680 it created a new set for us. 124 00:05:31,680 --> 00:05:33,783 It returned a new set. 125 00:05:35,040 --> 00:05:35,873 By the way, 126 00:05:35,873 --> 00:05:36,810 there's actually a shorthand 127 00:05:36,810 --> 00:05:39,270 that you can do here, in Python, 128 00:05:39,270 --> 00:05:41,880 which is the shift, 129 00:05:41,880 --> 00:05:45,030 and then the key above your enter, 130 00:05:45,030 --> 00:05:47,370 which is right here, this straight line. 131 00:05:47,370 --> 00:05:51,390 So shift, and then the back slash, 132 00:05:51,390 --> 00:05:52,500 but if you press shift, 133 00:05:52,500 --> 00:05:54,510 you'll get this little straight line. 134 00:05:54,510 --> 00:05:55,743 If you print this, 135 00:05:57,660 --> 00:05:59,550 remove the brackets here, 136 00:05:59,550 --> 00:06:00,633 let's run again, 137 00:06:02,370 --> 00:06:04,920 and you see that works, as well. 138 00:06:04,920 --> 00:06:05,753 And by the way, 139 00:06:05,753 --> 00:06:08,670 this also exists for intersection. 140 00:06:08,670 --> 00:06:10,530 Again, you don't see it that often, 141 00:06:10,530 --> 00:06:12,300 but with intersection, 142 00:06:12,300 --> 00:06:13,380 you can do the same thing 143 00:06:13,380 --> 00:06:16,353 where you just do the end, 144 00:06:17,340 --> 00:06:19,023 and if you do that, 145 00:06:21,750 --> 00:06:23,340 it gives you the intersection 146 00:06:23,340 --> 00:06:24,933 of my set and your set. 147 00:06:26,430 --> 00:06:29,430 Alright, I promised you this is gonna be short, 148 00:06:29,430 --> 00:06:34,430 so two other ones, issubset and issuperset. 149 00:06:35,310 --> 00:06:38,400 As you can imagine, my set, 150 00:06:38,400 --> 00:06:39,233 if let's say, 151 00:06:39,233 --> 00:06:41,463 my set only has four and five, 152 00:06:42,750 --> 00:06:45,810 and I say, hey, is my set, is that a subset, 153 00:06:45,810 --> 00:06:47,850 of your set? 154 00:06:47,850 --> 00:06:50,100 And I print this, 155 00:06:50,100 --> 00:06:53,340 if I click run, it's true. 156 00:06:53,340 --> 00:06:56,070 My set is a subset that is, 157 00:06:56,070 --> 00:06:57,256 it's inside of the circle, 158 00:06:57,256 --> 00:06:58,890 of your set, 159 00:06:58,890 --> 00:07:01,710 because four and five, is inside of it. 160 00:07:01,710 --> 00:07:04,920 So my set, the entirety of my set, 161 00:07:04,920 --> 00:07:07,800 is inside of this other set, 162 00:07:07,800 --> 00:07:09,333 which we call a subset. 163 00:07:10,860 --> 00:07:11,793 On the other hand, 164 00:07:12,630 --> 00:07:16,830 we also have this idea of superset. 165 00:07:16,830 --> 00:07:17,663 So if I do, 166 00:07:17,663 --> 00:07:21,903 instead of issubset, issuperset, 167 00:07:23,250 --> 00:07:25,080 I'll get false, 168 00:07:25,080 --> 00:07:26,490 because, well, my set, 169 00:07:26,490 --> 00:07:27,660 is not a superset, 170 00:07:27,660 --> 00:07:29,670 it doesn't encompass your set, 171 00:07:29,670 --> 00:07:31,443 it's the other way around. 172 00:07:32,430 --> 00:07:37,023 Your set is a superset of my set, 173 00:07:39,960 --> 00:07:43,080 because your set encompasses everything 174 00:07:43,080 --> 00:07:44,130 that my set has, 175 00:07:44,130 --> 00:07:45,753 which is four and five. 176 00:07:46,920 --> 00:07:48,870 Oh boy, that was a lot. 177 00:07:48,870 --> 00:07:50,070 And you're asking yourself, 178 00:07:50,070 --> 00:07:51,660 there is no way I'm going to 179 00:07:51,660 --> 00:07:53,430 memorize any of that. 180 00:07:53,430 --> 00:07:55,650 Well, good, I don't memorize 181 00:07:55,650 --> 00:07:57,150 any of this either. 182 00:07:57,150 --> 00:07:59,100 As a matter of fact, I usually go 183 00:07:59,100 --> 00:08:01,500 to the Python documentation, 184 00:08:01,500 --> 00:08:03,540 or just Google around. 185 00:08:03,540 --> 00:08:05,940 And because I know sets exist, 186 00:08:05,940 --> 00:08:07,500 if I ever wanna compare, 187 00:08:07,500 --> 00:08:09,870 let's say two sets, I know to just go here, 188 00:08:09,870 --> 00:08:12,120 and find the tool that I want. 189 00:08:12,120 --> 00:08:13,560 That's the key of a programmer 190 00:08:13,560 --> 00:08:15,450 that I keep mentioning over and over. 191 00:08:15,450 --> 00:08:16,950 It's not memorization. 192 00:08:16,950 --> 00:08:18,720 It's the ability to know 193 00:08:18,720 --> 00:08:20,640 that there's tools that you can use, 194 00:08:20,640 --> 00:08:22,740 and being able to Google for them. 195 00:08:22,740 --> 00:08:24,570 I know it doesn't sound exciting, 196 00:08:24,570 --> 00:08:27,210 because in movies you see everybody typing super fast, 197 00:08:27,210 --> 00:08:30,270 and coding everything, but that's just not real life. 198 00:08:30,270 --> 00:08:31,290 In real life, 199 00:08:31,290 --> 00:08:33,960 you just Google around, find solutions, 200 00:08:33,960 --> 00:08:36,330 because by now you know what sets are, 201 00:08:36,330 --> 00:08:37,740 you know what sets can do, 202 00:08:37,740 --> 00:08:40,200 so that you can Google for methods 203 00:08:40,200 --> 00:08:43,020 that are appropriate to your problem. 204 00:08:43,020 --> 00:08:44,490 I'll see you in the next one. 205 00:08:44,490 --> 00:08:45,323 Bye bye.