WEBVTT 1 00:00:00.160 --> 00:00:01.920 Hi guys, and welcome back. 2 00:00:01.920 --> 00:00:04.900 In this video, we will be learning about sets. 3 00:00:04.900 --> 00:00:09.000 Sets are another collection, like lists and tuples 4 00:00:09.000 --> 00:00:11.750 that contain multiple values inside them. 5 00:00:11.750 --> 00:00:14.397 But, of course, there has to be a difference between sets 6 00:00:14.397 --> 00:00:17.540 and the other two otherwise there would be no point to them. 7 00:00:17.540 --> 00:00:19.560 And the key difference is that sets 8 00:00:19.560 --> 00:00:22.930 don't hold order and 9 00:00:22.930 --> 00:00:26.310 sets don't contain duplicate elements. 10 00:00:26.310 --> 00:00:29.310 So, let's have a look at our art friends, 11 00:00:29.310 --> 00:00:31.130 our friends who study art. 12 00:00:31.130 --> 00:00:34.010 And let's say those are Rolf and Anne. 13 00:00:34.010 --> 00:00:36.280 Here, I've defined my first set. 14 00:00:36.280 --> 00:00:39.840 And you can denote a set by using these curly braces 15 00:00:39.840 --> 00:00:42.770 instead of the square brackets or the normal brackets. 16 00:00:42.770 --> 00:00:44.440 Square brackets for a list. 17 00:00:44.440 --> 00:00:46.830 Normal brackets for a tuple. 18 00:00:46.830 --> 00:00:49.140 By using curly braces, Python knows that 19 00:00:49.140 --> 00:00:51.170 you are defining a set. 20 00:00:51.170 --> 00:00:52.760 So, here we've got our first set. 21 00:00:52.760 --> 00:00:56.340 Art friends and now let's define science friends. 22 00:00:56.340 --> 00:00:58.033 So there's Jen and Charlie. 23 00:00:59.350 --> 00:01:01.830 If you want to add to a set, 24 00:01:01.830 --> 00:01:04.600 you just do something like art_friends.add, 25 00:01:04.600 --> 00:01:06.590 and then add Jen. 26 00:01:06.590 --> 00:01:07.423 What this is going to do, 27 00:01:07.423 --> 00:01:09.720 is it's going to add the Jen string 28 00:01:09.720 --> 00:01:11.740 into the art friends set. 29 00:01:11.740 --> 00:01:15.150 And it's going to sort of put it anywhere in that set. 30 00:01:15.150 --> 00:01:18.370 Remember, sets don't keep order so it could be anywhere. 31 00:01:18.370 --> 00:01:19.790 It could be at the start, it could be in the middle, 32 00:01:19.790 --> 00:01:21.150 it could be at the end. 33 00:01:21.150 --> 00:01:24.250 And there is no guarantee on the order being kept 34 00:01:24.250 --> 00:01:25.570 throughout the programme either. 35 00:01:25.570 --> 00:01:26.790 So whenever you print the set, 36 00:01:26.790 --> 00:01:28.440 it could be in a different order. 37 00:01:29.290 --> 00:01:30.547 So, let's print it out. 38 00:01:32.800 --> 00:01:34.300 And run it. 39 00:01:34.300 --> 00:01:35.720 You can see that, in this case, 40 00:01:35.720 --> 00:01:38.400 the Jen string has been printed at the start, 41 00:01:38.400 --> 00:01:40.270 then Anne, and then Rolf. 42 00:01:40.270 --> 00:01:41.903 If you try to do this again, 43 00:01:43.890 --> 00:01:46.640 you'll see that the same set gets printed out twice 44 00:01:46.640 --> 00:01:48.940 because we tried to add a value to the set 45 00:01:48.940 --> 00:01:51.530 that was already there, it didn't happen. 46 00:01:51.530 --> 00:01:53.850 Nothing happened, no error happens, 47 00:01:53.850 --> 00:01:55.750 just the value does not get added. 48 00:01:55.750 --> 00:01:57.390 It's already in there and sets do 49 00:01:57.390 --> 00:01:59.010 not contain duplicate elements, 50 00:01:59.010 --> 00:02:01.800 so nothing gets added to that set. 51 00:02:01.800 --> 00:02:03.350 You can remove things from a set though, 52 00:02:03.350 --> 00:02:04.853 by doing dot, remove. 53 00:02:06.360 --> 00:02:08.980 And now you can see that the second time we printed, 54 00:02:08.980 --> 00:02:11.803 Jen is gone because we removed her in line eight. 55 00:02:13.200 --> 00:02:16.640 So, this is how to work with sets in a very basic form. 56 00:02:16.640 --> 00:02:20.440 But, it doesn't show you why sets are useful. 57 00:02:20.440 --> 00:02:22.380 One of the key use cases about sets, 58 00:02:22.380 --> 00:02:25.820 is that it is very easy to compare one set to another. 59 00:02:25.820 --> 00:02:27.900 For example, ask sets. 60 00:02:27.900 --> 00:02:29.370 What elements are in this set 61 00:02:29.370 --> 00:02:31.230 that are not in this other set? 62 00:02:31.230 --> 00:02:34.850 Or, what elements are common between both sets? 63 00:02:34.850 --> 00:02:37.320 Things like that are why sets are useful. 64 00:02:37.320 --> 00:02:40.050 So let's look at those operations in the next video. 65 00:02:40.050 --> 00:02:41.053 I'll see you there.