1 00:00:00,700 --> 00:00:05,720 The concept of membership is a distinguishing feature of sets. 2 00:00:05,720 --> 00:00:09,670 Recall that an element of a set is called a member. 3 00:00:09,670 --> 00:00:13,955 The concept of membership testing means asking whether or 4 00:00:13,955 --> 00:00:16,500 not an element is a member of a set. 5 00:00:17,660 --> 00:00:23,220 In our last video, we performed membership testing by using the in keyword. 6 00:00:23,220 --> 00:00:28,940 We can also use the not in keywords to test for non-membership. 7 00:00:28,940 --> 00:00:33,920 And the answer is a boolean value, true or false. 8 00:00:33,920 --> 00:00:36,150 Is it there or not? 9 00:00:36,150 --> 00:00:39,830 Membership testing can be performed on one set. 10 00:00:39,830 --> 00:00:44,337 When there are two or more sets, membership testing can help us 11 00:00:44,337 --> 00:00:48,433 determine how different sets are related to each other. 12 00:00:48,433 --> 00:00:52,880 For example, what happens when all of the members of 13 00:00:52,880 --> 00:00:56,324 one set are also members of another set? 14 00:00:56,324 --> 00:01:01,547 A, E, I, O, and U are members of the set of vowels and 15 00:01:01,547 --> 00:01:07,375 also members of the set of letters of the English alphabet. 16 00:01:07,375 --> 00:01:14,273 The relationship between the two sets can be illustrated with an Euler diagram. 17 00:01:14,273 --> 00:01:20,750 In an Euler diagram, one circle is completely enveloped by the other. 18 00:01:20,750 --> 00:01:24,671 We can describe the relationship between the sets in two ways. 19 00:01:24,671 --> 00:01:29,745 The vowel set is a subset of the alphabet set and 20 00:01:29,745 --> 00:01:34,963 the alphabet set is a superset of the vowel set. 21 00:01:34,963 --> 00:01:37,929 Sets can also have no members in common. 22 00:01:37,929 --> 00:01:42,490 A set of digits would be represented by a separate circle. 23 00:01:42,490 --> 00:01:48,020 In the two circles, alphabet and digits do not touch each other. 24 00:01:48,020 --> 00:01:51,165 We say that they are disjoint sets. 25 00:01:54,966 --> 00:01:57,243 Which letters are vowels? 26 00:01:57,243 --> 00:02:00,563 This is a question of membership testing. 27 00:02:00,563 --> 00:02:02,595 We can use the in keyword to find out. 28 00:02:13,097 --> 00:02:16,068 Is A in vowels? 29 00:02:16,068 --> 00:02:18,550 It's true. 30 00:02:18,550 --> 00:02:19,907 How about B? 31 00:02:23,860 --> 00:02:27,024 So A is in vowels, but B is not. 32 00:02:27,024 --> 00:02:31,873 We can test the opposite by using the not in keywords. 33 00:02:42,303 --> 00:02:45,014 Is C not in vowels? 34 00:02:45,014 --> 00:02:47,156 It's true. 35 00:02:47,156 --> 00:02:47,874 How about D? 36 00:02:52,713 --> 00:03:00,221 So A is in vowels, B is not in vowels, and C and D are also not in vowels, and so on. 37 00:03:00,221 --> 00:03:03,287 That's membership testing. 38 00:03:03,287 --> 00:03:05,525 Now on to relations. 39 00:03:05,525 --> 00:03:10,062 Every letter that is a vowel can be found in the letters of the alphabet. 40 00:03:10,062 --> 00:03:14,194 Let's make a set of letters of the whole alphabet. 41 00:03:14,194 --> 00:03:19,114 Instead of typing out all of the letters one by one, I'm going to import 42 00:03:19,114 --> 00:03:23,966 the string module which has constants for situations just like this. 43 00:03:25,009 --> 00:03:28,527 The constant that I want is called ascii_lowercase. 44 00:03:35,692 --> 00:03:39,486 It's a string of lowercase letters, I can show you in the interpreter. 45 00:03:51,615 --> 00:03:53,234 Since a string is an iterable, 46 00:03:53,234 --> 00:03:56,111 I can use the set constructor function to make it a set. 47 00:04:05,473 --> 00:04:08,688 Now I have a set of all of the letters in the alphabet. 48 00:04:13,340 --> 00:04:17,886 Let's prove programmatically that all members of the vowel set are also 49 00:04:17,886 --> 00:04:19,760 members of the alphabet set. 50 00:04:23,060 --> 00:04:27,714 I can loop through every letter of vowels and perform a membership test. 51 00:04:34,598 --> 00:04:37,052 Is that letter in the alphabet? 52 00:04:37,052 --> 00:04:40,810 If it is, I'll have a flag and I can change it to true. 53 00:04:49,285 --> 00:04:51,095 If it isn't, change the flag to false. 54 00:04:55,945 --> 00:04:59,679 And it has to be true for every letter involved. 55 00:04:59,679 --> 00:05:01,292 So if it's false just once, we can break the loop. 56 00:05:05,687 --> 00:05:10,157 I'll turn my pseudocode into 57 00:05:10,157 --> 00:05:14,993 some implementation code now. 58 00:05:38,183 --> 00:05:42,791 And if my is_related flag is True, then I wanna print out 59 00:05:42,791 --> 00:05:47,694 a nice message to let me know that these two sets are related. 60 00:06:07,406 --> 00:06:09,102 Let's run the code and see what happens. 61 00:06:18,323 --> 00:06:20,801 Something went wrong here. 62 00:06:20,801 --> 00:06:27,616 So I think my loop logic is correct, so I'm gonna scroll up to continue debugging. 63 00:06:27,616 --> 00:06:33,448 And, right here on line 3, I have capital letter vowels, 64 00:06:33,448 --> 00:06:38,092 but on line 4, I'm using my lowercase constant, 65 00:06:38,092 --> 00:06:42,305 when really I need to have ascii_uppercase. 66 00:06:42,305 --> 00:06:46,275 Let's hop back into the terminal and 67 00:06:46,275 --> 00:06:51,277 interpreter mode to see what that looks like. 68 00:06:55,747 --> 00:06:59,806 A string of all uppercase letters, great. 69 00:06:59,806 --> 00:07:03,839 Now let's run my script again. 70 00:07:03,839 --> 00:07:08,254 So for every letter in the vowels, if the letter is in the alphabet, 71 00:07:08,254 --> 00:07:11,132 we can change the is_related flag to true. 72 00:07:11,132 --> 00:07:14,236 And if all of those letters are in the alphabet, 73 00:07:14,236 --> 00:07:18,512 then we'll print out this message that the two sets are related. 74 00:07:18,512 --> 00:07:22,210 It's related, yay! 75 00:07:22,210 --> 00:07:27,062 We can say that the vowel set is a subset of the alphabet set. 76 00:07:27,062 --> 00:07:31,189 I've proven it to you with a couple blocks of code, but 77 00:07:31,189 --> 00:07:36,150 you can also check by calling the built-in set method issubset. 78 00:07:36,150 --> 00:07:41,675 To demonstrate this method, I'm actually gonna open my script 79 00:07:41,675 --> 00:07:46,911 in interpreter mode, python3 -i set_membership.py. 80 00:07:46,911 --> 00:07:52,402 It's related, and I can check that 81 00:07:52,402 --> 00:07:58,269 my vowels.issubset(alphabet). 82 00:08:01,252 --> 00:08:03,637 When I return here, this should say true. 83 00:08:06,135 --> 00:08:10,293 Each and every vowel is contained in the set of all of 84 00:08:10,293 --> 00:08:13,786 the letters in the alphabet, it's true. 85 00:08:13,786 --> 00:08:19,251 Inversely, we can say that the alphabet set is a superset of the vowel set. 86 00:08:27,279 --> 00:08:32,669 It's True, out of all of the letters in the alphabet set, 87 00:08:32,669 --> 00:08:37,189 we can also find the letters that are our vowels. 88 00:08:37,189 --> 00:08:42,886 issubset and issuperset can also be performed with shorthand operators. 89 00:08:42,886 --> 00:08:45,273 Shorthand means less typing. 90 00:08:45,273 --> 00:08:51,211 Instead of issubset, we can use the less than or equal sign. 91 00:08:57,400 --> 00:09:03,036 It's true, vowels is a subset of the alphabet. 92 00:09:03,036 --> 00:09:07,474 And instead of issuperset, we can use the greater than or equal sign. 93 00:09:14,310 --> 00:09:17,028 It's True, still True. 94 00:09:17,028 --> 00:09:19,599 You can check out my teacher's notes for 95 00:09:19,599 --> 00:09:24,370 a handy reference of the shorthand operators in the rest of this workshop. 96 00:09:24,370 --> 00:09:29,836 Earlier, we used another comparison operator on sets, the double equal sign. 97 00:09:29,836 --> 00:09:34,205 Two sets are equal to each other only if they have the exact same membership. 98 00:09:45,799 --> 00:09:50,005 These three comparison operators will return true when 99 00:09:50,005 --> 00:09:52,249 comparing a set with itself. 100 00:09:52,249 --> 00:09:57,704 Let's make a set of digits using the string module. 101 00:09:57,704 --> 00:10:03,993 First, I'll exit out of my interpreter and I'll come back to my script. 102 00:10:03,993 --> 00:10:11,819 And I can get rid of this block right here since we're using a new example. 103 00:10:11,819 --> 00:10:14,712 We'll say digits = string.digits. 104 00:10:17,637 --> 00:10:19,361 And I can make this into a set. 105 00:10:25,100 --> 00:10:30,100 Digits and alphabets are sets that have no members in common. 106 00:10:30,100 --> 00:10:34,651 We can say that the two sets are disjointed, 107 00:10:34,651 --> 00:10:38,590 digits.isdisjoint(alphabet). 108 00:10:41,798 --> 00:10:42,966 We can print this. 109 00:10:48,469 --> 00:10:52,613 It's True, there's no shorthand operator for disjoint, so 110 00:10:52,613 --> 00:10:56,766 you'll just have to settle for using the method call instead. 111 00:10:56,766 --> 00:10:57,985 In the next video, 112 00:10:57,985 --> 00:11:02,340 we'll explore more about set relationships using Venn diagrams.