1 00:00:00,470 --> 00:00:01,230 Can you believe it? 2 00:00:01,820 --> 00:00:06,180 I hope I didn't put you to sleep, but we're almost there. 3 00:00:06,500 --> 00:00:11,480 We have one last main data type set and then we're done. 4 00:00:11,630 --> 00:00:12,200 I know. 5 00:00:12,200 --> 00:00:14,780 I know this isn't the most exciting topic. 6 00:00:15,080 --> 00:00:16,940 Trust me, this is the hardest part. 7 00:00:16,940 --> 00:00:21,890 When you're starting out and you're just learning these things without understanding, why do I need 8 00:00:21,890 --> 00:00:22,150 this? 9 00:00:22,160 --> 00:00:23,810 How am I going to build the next game? 10 00:00:23,810 --> 00:00:26,150 How am I going to build the next Netflix? 11 00:00:26,150 --> 00:00:29,920 How am I going to build the next coolest Apple app? 12 00:00:30,410 --> 00:00:33,650 You're looking at this and thinking, I just learned a bunch of theory. 13 00:00:33,660 --> 00:00:35,320 I feel like I'm in math class all over again. 14 00:00:35,960 --> 00:00:36,400 I know. 15 00:00:36,410 --> 00:00:36,890 Trust me. 16 00:00:36,890 --> 00:00:37,610 I went through this. 17 00:00:37,610 --> 00:00:39,530 I was a self-taught programmer as well. 18 00:00:39,620 --> 00:00:42,260 These are the Lego blocks that builds our foundation. 19 00:00:42,470 --> 00:00:45,590 Once we learn these, we can move on to new things. 20 00:00:45,590 --> 00:00:51,620 And you'll see that once you climb this little mountain, things become so much easier and you'll realize 21 00:00:51,620 --> 00:00:52,310 the power. 22 00:00:52,490 --> 00:00:58,370 When we start building these projects toward the end of the class where you're going to say, oh, all 23 00:00:58,370 --> 00:01:03,500 these blocks that we learned, all this frankly boring part is all going to make sense. 24 00:01:04,310 --> 00:01:05,570 So hang in there. 25 00:01:05,570 --> 00:01:06,770 It's supposed to be hard. 26 00:01:06,950 --> 00:01:12,920 It's supposed to be tough, but you'll see very soon why this is so powerful and why what we've just 27 00:01:12,920 --> 00:01:18,860 learned is going to allow us to build some really awesome things and create some really awesome programs. 28 00:01:19,460 --> 00:01:27,740 So let's finish off with the last one, the last data type, the last data structure that we'll see 29 00:01:27,920 --> 00:01:30,740 in Python, at least from the main ones. 30 00:01:32,180 --> 00:01:40,550 And it's called set and sets are simply unordered collections of unique objects. 31 00:01:41,270 --> 00:01:41,540 Hmm. 32 00:01:41,780 --> 00:01:42,460 Let's have a look. 33 00:01:42,830 --> 00:01:51,290 Let's create a mind set and my set we create with curly brackets like a dictionary, but instead of 34 00:01:51,290 --> 00:01:53,960 a dictionary, we don't have key value pairs. 35 00:01:54,050 --> 00:01:59,120 Instead, we just have values like one, two, three, four or five. 36 00:02:00,480 --> 00:02:07,530 That's it, that's a set we just created a set, but remember, the definition I gave you set is an 37 00:02:07,530 --> 00:02:10,950 unordered collection of unique objects. 38 00:02:11,850 --> 00:02:12,540 What does that mean? 39 00:02:12,810 --> 00:02:18,120 Well, if I print this and say my set and I click run. 40 00:02:19,110 --> 00:02:25,350 I get a set, but if I had, let's say, another five in here and I click run. 41 00:02:26,910 --> 00:02:35,390 It only returns the unique sets or unique items you see in a set, there's no duplicates. 42 00:02:35,400 --> 00:02:37,620 Everything has to be unique. 43 00:02:38,640 --> 00:02:45,660 So this fire just never gets added to a set, for example, you can add things to a set like my set 44 00:02:46,440 --> 00:02:50,250 and then I can say add and give it a value like one hundred. 45 00:02:51,200 --> 00:02:57,560 And then let's say I do my set dot ad and then give it to if I click, run here. 46 00:02:58,670 --> 00:03:05,450 I see that I was able to add one hundred, but two well, two wasn't really added because my said already 47 00:03:05,450 --> 00:03:08,020 contained that data. 48 00:03:08,690 --> 00:03:10,070 It's unordered. 49 00:03:10,680 --> 00:03:15,950 I mean, we've created one, two, three, four or five here, but there's no real order to it. 50 00:03:16,610 --> 00:03:21,020 There's no bookshelf in our memory space that is right next to each other. 51 00:03:21,180 --> 00:03:23,270 These are all over the place in memory. 52 00:03:23,270 --> 00:03:28,040 But a set is able to find them because they're all unique. 53 00:03:28,250 --> 00:03:30,590 So they're all in just one location in memory. 54 00:03:31,560 --> 00:03:36,240 So let me ask you this, and this is going to be a fun little exercise. 55 00:03:37,290 --> 00:03:39,570 If I gave you an array. 56 00:03:40,590 --> 00:03:44,490 And let's say this array contained, well, this right here. 57 00:03:45,730 --> 00:03:53,350 And I want you to return an array with no duplicates, so I want everything in let's say let's call 58 00:03:53,350 --> 00:03:56,080 it my list. 59 00:03:57,700 --> 00:04:00,040 And a programmer has already created this. 60 00:04:01,100 --> 00:04:13,500 OK, and then your task is to create or return a list or a collection that has only unique items. 61 00:04:14,120 --> 00:04:16,520 How would you go about converting this into a set? 62 00:04:17,330 --> 00:04:19,000 Well, we've seen this before. 63 00:04:19,010 --> 00:04:25,790 We have a set function and we simply wrap my list in that set. 64 00:04:27,110 --> 00:04:28,160 And that's printed out. 65 00:04:29,720 --> 00:04:30,500 I click, run. 66 00:04:31,940 --> 00:04:32,460 Check it out. 67 00:04:32,780 --> 00:04:35,900 We have formed a new set from a list. 68 00:04:37,340 --> 00:04:41,480 And I've removed all duplicate values, when would this be useful? 69 00:04:42,050 --> 00:04:48,680 Imagine if we had usernames, right, or email addresses where collecting email addresses on our startup 70 00:04:48,680 --> 00:04:52,030 page, but we don't want to have duplicate emails. 71 00:04:52,070 --> 00:04:57,380 We might want to convert this a list of emails to a set and remove any duplicates. 72 00:04:57,380 --> 00:04:59,540 So we're not sending emails over and over. 73 00:04:59,720 --> 00:05:00,910 So that's really cool. 74 00:05:01,880 --> 00:05:08,750 What about this, can I let's change this into a set, so let's call it my set. 75 00:05:09,910 --> 00:05:11,610 It's going to change this into a set. 76 00:05:11,850 --> 00:05:12,530 There you go. 77 00:05:13,760 --> 00:05:18,920 And then what if I wanted to access my set an index of zero? 78 00:05:20,400 --> 00:05:23,160 Hmm, set object does not support indexing. 79 00:05:23,490 --> 00:05:31,320 Well, again, you can think of it more as a dictionary in order to access a set, we have to grab by 80 00:05:31,320 --> 00:05:33,060 the item that's in. 81 00:05:33,780 --> 00:05:36,900 That's the key to getting the information from memory. 82 00:05:37,560 --> 00:05:48,030 The way we would check if something exists is we simply say, hey, is one in my set and we run this. 83 00:05:49,380 --> 00:05:50,820 And we get to. 84 00:05:51,720 --> 00:06:02,070 We can also do the length of my set and we get five again, nothing too crazy, but one, two, three, 85 00:06:02,070 --> 00:06:02,780 four, five. 86 00:06:02,830 --> 00:06:07,350 Remember, it only counts the unique things because this will never gets entered. 87 00:06:07,350 --> 00:06:08,010 It's a set. 88 00:06:08,730 --> 00:06:12,300 And alternatively, we can also convert this into a list. 89 00:06:12,490 --> 00:06:18,540 If I click run here, you'll see that I now have my set as a list. 90 00:06:19,710 --> 00:06:28,770 And then we can also do, let's say, my set daughter copy and actually copy my set into something, 91 00:06:28,800 --> 00:06:29,850 let's say new. 92 00:06:31,010 --> 00:06:39,140 Or new set, and then this will be completely different or a new copy, then this original one. 93 00:06:39,830 --> 00:06:43,340 So let's test this up, this new set. 94 00:06:44,920 --> 00:06:53,050 If I use a method that we may have seen before, which is my set dot clear, which as you can imagine, 95 00:06:53,050 --> 00:06:55,390 clears the set and I click run. 96 00:06:57,090 --> 00:07:06,780 I have that new set, but when I try and print are all set, which was my set, it's going to be empty, 97 00:07:06,780 --> 00:07:11,850 an empty set, but the true power of sets comes in. 98 00:07:11,850 --> 00:07:19,470 The next couple methods that I'm going to show you and sets have quite a few methods, but don't worry, 99 00:07:19,800 --> 00:07:22,110 most of them are quite similar. 100 00:07:22,120 --> 00:07:24,000 So let's explore that in the next video.