1 00:00:00,750 --> 00:00:03,440 Sets have several different operations that they can do. 2 00:00:03,440 --> 00:00:05,140 I'll talk briefly about each of them in this video, 3 00:00:05,140 --> 00:00:07,010 and we'll use them together in work spaces. 4 00:00:07,010 --> 00:00:10,100 But you should check the documentation which I've linked in the teacher's notes, 5 00:00:10,100 --> 00:00:11,350 and practice on your own. 6 00:00:11,350 --> 00:00:13,850 They can be really tricky until you get used to them. 7 00:00:13,850 --> 00:00:15,530 Let's pretend we have two sets. 8 00:00:15,530 --> 00:00:18,340 The first set will be the first ten positive whole numbers. 9 00:00:18,340 --> 00:00:20,910 The second will be the first ten prime numbers. 10 00:00:20,910 --> 00:00:22,490 We'll put them in two circles. 11 00:00:22,490 --> 00:00:24,630 Hey, that kind of looks like a Venn diagram. 12 00:00:24,630 --> 00:00:28,760 Well it should because Venn diagrams and sets are pretty tightly linked. 13 00:00:28,760 --> 00:00:30,800 The first operation to talk about is union. 14 00:00:30,800 --> 00:00:33,250 This is the combination of two or more sets. 15 00:00:33,250 --> 00:00:35,570 In this case, we'd get each number in both sets. 16 00:00:35,570 --> 00:00:39,740 But since sets are unique, we wouldn't get multiple ones, or fives, or anything. 17 00:00:39,740 --> 00:00:41,120 Next is difference. 18 00:00:41,120 --> 00:00:45,260 This operation finds everything that's in the first set, but not in the second. 19 00:00:45,260 --> 00:00:48,600 There's also a symmetric difference which is everything that is unique to 20 00:00:48,600 --> 00:00:49,550 either set. 21 00:00:49,550 --> 00:00:51,050 So, no shared numbers. 22 00:00:51,050 --> 00:00:52,510 Sorry primes below ten. 23 00:00:52,510 --> 00:00:54,740 And lastly, we have the intersection. 24 00:00:54,740 --> 00:00:59,430 An intersection of sets gives a new set of all items that are in both sets. 25 00:00:59,430 --> 00:01:02,330 The intersection of these two sets gives us only the primes below ten. 26 00:01:03,550 --> 00:01:07,650 In python, we can do all of these operations as both methods and operators. 27 00:01:07,650 --> 00:01:10,090 Let's go look at them in work spaces. 28 00:01:10,090 --> 00:01:12,781 So I'm actually gonna go ahead and recreate those two sets. 29 00:01:12,781 --> 00:01:17,260 So set1 was a set of the first ten numbers. 30 00:01:17,260 --> 00:01:18,606 The first 10 positive numbers. 31 00:01:18,606 --> 00:01:22,250 And set2 was 1,2,3,5,7,11,13,17,19, and 23. 32 00:01:22,250 --> 00:01:27,109 So let's get the union 33 00:01:27,109 --> 00:01:31,968 of these two sets using 34 00:01:31,968 --> 00:01:36,068 the union method. 35 00:01:36,068 --> 00:01:41,780 So set1.union(set2), and we get a new set. 36 00:01:41,780 --> 00:01:44,850 Now, if I had done set1 dot update set2, 37 00:01:44,850 --> 00:01:47,940 it would have effectively done the same thing, right? 38 00:01:47,940 --> 00:01:49,737 But it would have changed set one. 39 00:01:49,737 --> 00:01:53,702 Calling union does not change set1 or set2. 40 00:01:53,702 --> 00:01:55,673 See, they're still the same. 41 00:01:55,673 --> 00:01:56,460 So that's cool. 42 00:01:56,460 --> 00:02:00,910 Now, I can do the same thing with the union operand which is the pipe character. 43 00:02:00,910 --> 00:02:04,420 So set1 | set2, there we go, same output. 44 00:02:04,420 --> 00:02:08,185 Now, to get the difference between the two sets, I can use the difference method. 45 00:02:08,185 --> 00:02:15,210 set1.difference(set2), there is what's different between those two. 46 00:02:15,210 --> 00:02:21,716 Or I can turn that around set2.difference(set1). 47 00:02:21,716 --> 00:02:27,032 And there's the difference on those two, 19, 17, 11, 13, and 23. 48 00:02:27,032 --> 00:02:29,721 The difference operator is a hyphen. 49 00:02:29,721 --> 00:02:33,500 So set1- set2 or set2- set1. 50 00:02:34,500 --> 00:02:36,070 For the symmetric difference, or 51 00:02:36,070 --> 00:02:40,308 the things that are unique to each side, I use the carrot symbol. 52 00:02:40,308 --> 00:02:43,870 It looks kinda like the top of the house or an arrow pointing up. 53 00:02:43,870 --> 00:02:50,191 So set1 ^ set2, gets me the things that are unique to each side. 54 00:02:50,191 --> 00:02:52,958 And I can, of course, do this with a method as well. 55 00:02:52,958 --> 00:03:00,320 So set2.symmetric_difference(set1), and I misspell this one constantly. 56 00:03:01,390 --> 00:03:02,640 There we go. 57 00:03:02,640 --> 00:03:07,222 And last, but definitely not least, is the intersection of the sets. 58 00:03:07,222 --> 00:03:14,153 So set1.intersection(set2), has those items in it. 59 00:03:14,153 --> 00:03:17,890 And the operand for that is the ampersand. 60 00:03:17,890 --> 00:03:21,680 So set1 & set2, gets the same numbers. 61 00:03:21,680 --> 00:03:22,990 Those are all pretty handy. 62 00:03:24,000 --> 00:03:27,250 For union and symmetric difference, the order of the sets isn't important 63 00:03:27,250 --> 00:03:30,610 because valid items from both sets get put into the final set. 64 00:03:30,610 --> 00:03:32,870 For the other operators though, order matters. 65 00:03:32,870 --> 00:03:35,160 Before you jump in the core challenges in the next video, 66 00:03:35,160 --> 00:03:37,950 take a little while to play with sets on your own, and look at the docs. 67 00:03:37,950 --> 00:03:42,416 Be sure to look at how to determine if one set is a superset or subset of another, or 68 00:03:42,416 --> 00:03:44,669 if two sets are completely disjointed. 69 00:03:44,669 --> 00:03:48,260 That last one is often really useful to make sure the two sets have no overlap.