1 00:00:00,480 --> 00:00:03,150 Hello and welcome to this video. 2 00:00:03,180 --> 00:00:08,350 In this video I'm going to introduce you to a pilot on set. 3 00:00:08,490 --> 00:00:21,370 The set is a type of data structure in python what is a python set is set is basically a collection 4 00:00:21,520 --> 00:00:23,240 of values. 5 00:00:23,500 --> 00:00:27,590 The values in this set are not ordered. 6 00:00:27,610 --> 00:00:34,960 That means they are not in any particular order and the values in the set are not indexed. 7 00:00:34,960 --> 00:00:37,250 This is because they are not ordered. 8 00:00:37,270 --> 00:00:40,890 So it's difficult to index them. 9 00:00:41,050 --> 00:00:46,590 The set allows you to add values to it. 10 00:00:46,660 --> 00:00:53,200 However you cannot change the values that are already in the set. 11 00:00:53,200 --> 00:00:54,460 This is very important. 12 00:00:54,460 --> 00:01:01,870 You can add values to a set but you cannot change existing values. 13 00:01:01,960 --> 00:01:07,600 One way of creating a set is by giving the set a name. 14 00:01:07,600 --> 00:01:17,350 Try to give your set a descriptive name that reflects the type of values it stalls and then you add 15 00:01:17,500 --> 00:01:25,090 curly braces and inside the curly braces is where you indicate the values that are going to be stored 16 00:01:25,300 --> 00:01:27,460 in the set. 17 00:01:27,460 --> 00:01:30,800 I have got my text editor opened. 18 00:01:30,840 --> 00:01:34,570 I'm going to be using the text edit or code item. 19 00:01:34,720 --> 00:01:40,720 I've also created a python file and I've coded my set dot P Y. 20 00:01:40,750 --> 00:01:42,840 So that's the name of the file. 21 00:01:43,000 --> 00:01:47,850 He must have a dot P Y to make it a python file. 22 00:01:47,950 --> 00:01:50,990 So I'm going to create a set. 23 00:01:51,220 --> 00:01:58,300 So I give the set a name I'm going to call it fruits and I use the quotes to to assign values and the 24 00:01:58,300 --> 00:02:02,370 values has to be placed in inside curly braces. 25 00:02:02,380 --> 00:02:07,800 So I'll now add the values the first value I'm going to has it's going to be grapes. 26 00:02:07,870 --> 00:02:11,290 You separate a value with a comma. 27 00:02:11,310 --> 00:02:16,590 Now I'm going to add another one called apples onto a comma. 28 00:02:16,610 --> 00:02:21,190 Now add one more and I'll call this berries. 29 00:02:21,400 --> 00:02:32,980 So that basically creates a set it is difficult to access the individual elements in a set because they 30 00:02:32,980 --> 00:02:37,500 are not indexed so they are not in any particular order the orders can change. 31 00:02:37,570 --> 00:02:42,400 So you can't access the value from the index because they are not index. 32 00:02:42,790 --> 00:02:51,550 However you can loop through and print out all the elements in a set and you can do that using the for 33 00:02:51,550 --> 00:02:51,880 loop. 34 00:02:51,880 --> 00:03:00,270 So to do that I can just say for x in fruits. 35 00:03:00,310 --> 00:03:00,870 Okay. 36 00:03:00,880 --> 00:03:03,250 And doing the column. 37 00:03:03,640 --> 00:03:05,790 So the x represent each value. 38 00:03:05,880 --> 00:03:16,690 I can still say print x and what that will do it will print all the elements inside the set. 39 00:03:16,690 --> 00:03:17,950 So let's see how that works. 40 00:03:17,950 --> 00:03:25,300 Let me run this python file inside my command prompt so I've navigated to my desktop which is where 41 00:03:25,300 --> 00:03:26,910 the python file is. 42 00:03:26,920 --> 00:03:35,210 So in order to run the file I just call it by its name so I'll save my set dot P Y and our press pointer 43 00:03:35,800 --> 00:03:42,720 and you can see it prints out all the elements inside the set. 44 00:03:42,790 --> 00:03:49,190 Another way you can create a set is by using a constructor function. 45 00:03:49,300 --> 00:03:54,790 So the constructor basically is a special type of function used to create objects. 46 00:03:54,790 --> 00:03:58,690 So if I wanted to create a set code animals. 47 00:03:58,780 --> 00:04:03,010 This is how I would represent it when you're using the constructor function. 48 00:04:03,010 --> 00:04:06,500 Notice you have double parenthesis. 49 00:04:06,520 --> 00:04:11,680 So I'm going to create a set using a construct of function. 50 00:04:11,770 --> 00:04:13,270 So you start with a name. 51 00:04:13,300 --> 00:04:15,800 I want to call it and we'll call it animals. 52 00:04:15,810 --> 00:04:24,460 Now say equals two and I'm gonna have double parentheses inside the inner parentheses are specify the 53 00:04:24,460 --> 00:04:32,000 values that I want to add to the set first values a line second value. 54 00:04:32,020 --> 00:04:34,800 I will say Tiger. 55 00:04:35,140 --> 00:04:40,550 I'll add one more and I'll make this one a bear. 56 00:04:40,660 --> 00:04:49,480 So basically created another set by using a constructor function. 57 00:04:49,480 --> 00:04:57,960 Now if I wanted to print the values of this set on the screen I can do what I've done here. 58 00:04:58,000 --> 00:05:05,590 On line two or three just do the same thing here and that will loop through and print out the elements 59 00:05:05,980 --> 00:05:09,040 inside that set. 60 00:05:09,040 --> 00:05:17,960 If you want to count the values in a set you can do that using a special built in function code Len. 61 00:05:18,130 --> 00:05:28,330 If you just add the name of the set inside the line function that will print out the values in the set. 62 00:05:29,110 --> 00:05:34,530 So let's try and print out the values inside the set code animals. 63 00:05:34,690 --> 00:05:41,350 So the way we'll do that is to do a print inside that print function we had or not up onto a code. 64 00:05:41,350 --> 00:05:51,270 Len and inside our Len function will pass in the name of the set which is animals which chose to control 65 00:05:51,330 --> 00:05:53,410 as to save on the window. 66 00:05:53,580 --> 00:05:55,750 If you on the Mac just to comment yes. 67 00:05:55,770 --> 00:05:58,040 And that will save the file. 68 00:05:58,110 --> 00:06:07,350 So now if I run my file again which is my set the P Way it will execute the code. 69 00:06:07,440 --> 00:06:14,220 You can see here this print x here print all the values from the first set which is fruits. 70 00:06:14,670 --> 00:06:24,480 And this line of code three here basically is this here way is print in the length of the values in 71 00:06:24,480 --> 00:06:29,870 this set called animals you can see there are three in the bed the tiger and the lion. 72 00:06:29,870 --> 00:06:34,760 And that is what this value 3 represents. 73 00:06:34,770 --> 00:06:36,530 So that's set for these videos. 74 00:06:36,570 --> 00:06:37,570 Thanks for watching.