1 00:00:01,020 --> 00:00:05,850 We have just learned how to make sets with literal syntax. 2 00:00:05,850 --> 00:00:11,033 We experimented with making Singleton sets with one element, 3 00:00:11,033 --> 00:00:13,487 and sets with many elements. 4 00:00:13,487 --> 00:00:17,979 We learned that sets can only contain immutable data. 5 00:00:17,979 --> 00:00:23,388 Let's try using what we've learned so far to make an empty set. 6 00:00:23,388 --> 00:00:28,625 On line one here, I'll declare a new variable empty_set. 7 00:00:28,625 --> 00:00:36,027 And I'll assign it to an empty set literal, which is a pair of curly braces. 8 00:00:36,027 --> 00:00:41,517 Then I'll print out my empty set and let's see what we get. 9 00:00:44,122 --> 00:00:48,357 So my return value here is a pair of curly braces, but 10 00:00:48,357 --> 00:00:52,512 I wanna make super sure that this is actually a set. 11 00:00:52,512 --> 00:00:55,737 So what I'll do is, I'll use the type function. 12 00:00:59,696 --> 00:01:05,797 So I can see the return value of the type that my empty set actually is. 13 00:01:05,797 --> 00:01:09,099 I'll run my script again. 14 00:01:09,099 --> 00:01:16,369 And this is actually a dictionary, so we can't make an empty set with curly braces. 15 00:01:16,369 --> 00:01:21,972 To make an empty set, we actually have to use the set constructor function. 16 00:01:21,972 --> 00:01:25,591 So I'll go all the way up here to line one again and 17 00:01:25,591 --> 00:01:30,537 I'll reassign my variable to use this set constructor function, 18 00:01:30,537 --> 00:01:35,058 which is just the word set followed by a pair of parentheses. 19 00:01:35,058 --> 00:01:40,497 Now I'll run my script again. 20 00:01:40,497 --> 00:01:42,439 And there's two things to notice here. 21 00:01:42,439 --> 00:01:47,079 First, when I print out a line two my empty set, 22 00:01:47,079 --> 00:01:53,823 the return value is exactly the same as my set constructor function. 23 00:01:53,823 --> 00:01:58,473 If you ever see this set function as a return value, 24 00:01:58,473 --> 00:02:01,730 it means that that's an empty set. 25 00:02:01,730 --> 00:02:06,615 The type returns the class set. 26 00:02:06,615 --> 00:02:09,346 So I know for sure that this is a set. 27 00:02:09,346 --> 00:02:11,928 I know two ways that this is a set. 28 00:02:14,208 --> 00:02:19,387 The set constructor function can also take any iterable as an argument. 29 00:02:19,387 --> 00:02:23,113 So right now we've just made an empty set, 30 00:02:23,113 --> 00:02:27,470 now we're going to make a set with many elements. 31 00:02:27,470 --> 00:02:29,449 We're going to pass in an iterable. 32 00:02:29,449 --> 00:02:34,939 And recall that an iterable is the type that you can use in a for loop. 33 00:02:34,939 --> 00:02:39,082 I'm gonna use my favorite data type that's an iterable, which is a string. 34 00:02:39,082 --> 00:02:43,934 I have a sentence here, My favorite data type 35 00:02:43,934 --> 00:02:47,902 is the string, exclamation point. 36 00:02:47,902 --> 00:02:50,338 Really excited about it. 37 00:02:50,338 --> 00:02:54,559 And now I can use the set function to convert my 38 00:02:54,559 --> 00:02:59,351 string into a set of letters, letters equals set. 39 00:02:59,351 --> 00:03:03,632 And I'll pass in my sentence. 40 00:03:03,632 --> 00:03:09,975 Now let's print out my letters and see what is happening here. 41 00:03:09,975 --> 00:03:11,207 What are we gonna get? 42 00:03:18,330 --> 00:03:22,344 All right, so what happened here? 43 00:03:22,344 --> 00:03:26,838 The set function went through every letter of my sentence, 44 00:03:26,838 --> 00:03:31,776 it looped through each element of the string, which is a character. 45 00:03:31,776 --> 00:03:38,943 And then it added in every letter, every character into the set, 46 00:03:38,943 --> 00:03:42,996 but it also removed the duplicates. 47 00:03:42,996 --> 00:03:45,613 I wanna prove this to you, and I can show you two ways, right? 48 00:03:45,613 --> 00:03:48,621 The first way is by visually comparing. 49 00:03:48,621 --> 00:03:51,954 I see here that there's one space 50 00:03:51,954 --> 00:03:56,606 character that is a member of this letter set. 51 00:03:56,606 --> 00:04:03,561 And then my sentence string there is one, two, three, four, five, and six spaces. 52 00:04:03,561 --> 00:04:07,280 So only one space got added in. 53 00:04:07,280 --> 00:04:12,180 And I can prove this to programmatically as well by comparing 54 00:04:12,180 --> 00:04:17,092 the length of my sentence versus the length of my letter set. 55 00:04:17,092 --> 00:04:19,199 So let's do that. 56 00:04:19,199 --> 00:04:26,105 Len letters less than len sentence. 57 00:04:26,105 --> 00:04:31,145 And if this is true, then we'll know that 58 00:04:31,145 --> 00:04:39,798 some of the elements of the sentence were not added into the letter set. 59 00:04:39,798 --> 00:04:44,533 So I'll run this again and let me comment out my older print statements here. 60 00:04:49,354 --> 00:04:54,647 So it's true! There are less members in the letter 61 00:04:54,647 --> 00:04:59,564 set than there are elements in the sentence. 62 00:05:02,245 --> 00:05:06,283 Let's try another example with real-world objects. 63 00:05:12,128 --> 00:05:17,278 Colors are a good example of data that we can collect into a set because they 64 00:05:17,278 --> 00:05:23,028 are distinctly unique and it doesn't usually matter what order we collect them. 65 00:05:23,028 --> 00:05:26,157 So let's make a list of primary colors. 66 00:05:29,129 --> 00:05:36,092 We'll have red, yellow and blue. 67 00:05:39,591 --> 00:05:44,013 I can convert this list into a set with the set constructor function 68 00:05:44,013 --> 00:05:51,746 similarly to how the str and int functions that convert one data type to another. 69 00:05:57,393 --> 00:06:01,041 And now if I print out my primary colors, 70 00:06:06,260 --> 00:06:08,732 I can see that I have a set. 71 00:06:08,732 --> 00:06:13,691 And remember, if the order of your colors are different than mine, 72 00:06:13,691 --> 00:06:17,006 it's totally okay, order does not matter. 73 00:06:17,006 --> 00:06:21,681 I'll run my script again to show you that it comes up 74 00:06:21,681 --> 00:06:25,829 differently when I run it another two times. 75 00:06:25,829 --> 00:06:30,977 So we've just used a string, a list. 76 00:06:30,977 --> 00:06:33,610 How about we pass in a tuple to the set function? 77 00:06:33,610 --> 00:06:35,968 A tuple is also iterable. 78 00:06:35,968 --> 00:06:38,679 This time we'll have secondary colors. 79 00:06:41,848 --> 00:06:45,457 We'll have tuple, 80 00:06:45,457 --> 00:06:49,916 which is parentheses and 81 00:06:49,916 --> 00:06:56,072 we'll call this one with orange, 82 00:06:56,072 --> 00:06:59,905 purple, and green. 83 00:06:59,905 --> 00:07:03,168 Close this parentheses, there we go. 84 00:07:03,168 --> 00:07:07,007 And we will do this the same way. 85 00:07:13,483 --> 00:07:17,727 So I've got my tuple of secondary colors and 86 00:07:17,727 --> 00:07:22,897 passing in the tuple to my set constructor function. 87 00:07:22,897 --> 00:07:28,900 To convert it from a tuple into a set, I'll print out my secondary colors. 88 00:07:35,743 --> 00:07:38,371 Orange, purple, or green, nice. 89 00:07:38,371 --> 00:07:42,663 That one came in the same order as I typed it out, but if I run it again, 90 00:07:42,663 --> 00:07:46,529 it will come out in a different order, so non-deterministic. 91 00:07:46,529 --> 00:07:51,409 Ranges can also be passed to the set function to quickly 92 00:07:51,409 --> 00:07:53,646 build sets of integers. 93 00:07:53,646 --> 00:07:57,564 Odds, set, range and 94 00:07:57,564 --> 00:08:04,574 I'll go from 1 to 10 stepping by 2 and 95 00:08:04,574 --> 00:08:10,155 I can also do the same for evens. 96 00:08:10,155 --> 00:08:17,037 This time I'll start at number 2, we'll go up to 11 stepping by 2. 97 00:08:17,037 --> 00:08:19,721 And let's print these out. 98 00:08:19,721 --> 00:08:24,802 Print(odds), print(evens). 99 00:08:29,652 --> 00:08:31,601 I'll run my script again. 100 00:08:31,601 --> 00:08:33,897 There we go. 101 00:08:33,897 --> 00:08:42,609 I've mentioned that the return values of sets are non-deterministic, but 102 00:08:42,609 --> 00:08:48,287 sometimes they always appear in the same order. 103 00:08:48,287 --> 00:08:53,552 The reason for this has to do with the built-in hashable 104 00:08:53,552 --> 00:08:58,002 attribute that many Python objects do have. 105 00:08:58,002 --> 00:09:04,054 You saw this in the last video when I tried to print out my Boolean set and 106 00:09:04,054 --> 00:09:07,961 it didn't change the order like the other ones. 107 00:09:07,961 --> 00:09:12,191 So some of the built-in data types like integers or 108 00:09:12,191 --> 00:09:17,453 booleans or the none type, they will appear in the same order, 109 00:09:17,453 --> 00:09:23,024 but for other things like strings, tuples, mixed data types, 110 00:09:23,024 --> 00:09:27,281 the order will come out to be non deterministic. 111 00:09:27,281 --> 00:09:34,220 In the last video, I demonstrated how set literals cannot contain a dictionary. 112 00:09:34,220 --> 00:09:39,031 A dictionary can be passed to the set constructor function, but 113 00:09:39,031 --> 00:09:42,514 the behavior is not quite what you'd expect. 114 00:09:42,514 --> 00:09:43,160 I'll show you. 115 00:09:45,598 --> 00:09:48,834 First, I'll make a dictionary of groceries. 116 00:09:51,143 --> 00:09:56,863 We'll have milk, and 1 milk, and some bread. 117 00:09:56,863 --> 00:09:59,120 We'll have 2 breads. 118 00:10:02,282 --> 00:10:09,481 Now when I pass my groceries dictionary to the set constructor function and 119 00:10:09,481 --> 00:10:15,660 I print out that result, let's check what the return value is. 120 00:10:19,014 --> 00:10:25,374 Bread and milk, my grocery set just has the keys of bread and milk. 121 00:10:25,374 --> 00:10:28,482 Since dictionaries are key value pairs, 122 00:10:28,482 --> 00:10:33,025 the set function will only make a set of the dictionary keys. 123 00:10:33,025 --> 00:10:37,170 Check out my teacher's notes for more details about dictionaries and sets.