Hello and welcome to this video.
In this video I'm going to introduce you to a pilot on set.
The set is a type of data structure in python what is a python set is set is basically a collection
of values.
The values in this set are not ordered.
That means they are not in any particular order and the values in the set are not indexed.
This is because they are not ordered.
So it's difficult to index them.
The set allows you to add values to it.
However you cannot change the values that are already in the set.
This is very important.
You can add values to a set but you cannot change existing values.
One way of creating a set is by giving the set a name.
Try to give your set a descriptive name that reflects the type of values it stalls and then you add
curly braces and inside the curly braces is where you indicate the values that are going to be stored
in the set.
I have got my text editor opened.
I'm going to be using the text edit or code item.
I've also created a python file and I've coded my set dot P Y.
So that's the name of the file.
He must have a dot P Y to make it a python file.
So I'm going to create a set.
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
values has to be placed in inside curly braces.
So I'll now add the values the first value I'm going to has it's going to be grapes.
You separate a value with a comma.
Now I'm going to add another one called apples onto a comma.
Now add one more and I'll call this berries.
So that basically creates a set it is difficult to access the individual elements in a set because they
are not indexed so they are not in any particular order the orders can change.
So you can't access the value from the index because they are not index.
However you can loop through and print out all the elements in a set and you can do that using the for
loop.
So to do that I can just say for x in fruits.
Okay.
And doing the column.
So the x represent each value.
I can still say print x and what that will do it will print all the elements inside the set.
So let's see how that works.
Let me run this python file inside my command prompt so I've navigated to my desktop which is where
the python file is.
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
and you can see it prints out all the elements inside the set.
Another way you can create a set is by using a constructor function.
So the constructor basically is a special type of function used to create objects.
So if I wanted to create a set code animals.
This is how I would represent it when you're using the constructor function.
Notice you have double parenthesis.
So I'm going to create a set using a construct of function.
So you start with a name.
I want to call it and we'll call it animals.
Now say equals two and I'm gonna have double parentheses inside the inner parentheses are specify the
values that I want to add to the set first values a line second value.
I will say Tiger.
I'll add one more and I'll make this one a bear.
So basically created another set by using a constructor function.
Now if I wanted to print the values of this set on the screen I can do what I've done here.
On line two or three just do the same thing here and that will loop through and print out the elements
inside that set.
If you want to count the values in a set you can do that using a special built in function code Len.
If you just add the name of the set inside the line function that will print out the values in the set.
So let's try and print out the values inside the set code animals.
So the way we'll do that is to do a print inside that print function we had or not up onto a code.
Len and inside our Len function will pass in the name of the set which is animals which chose to control
as to save on the window.
If you on the Mac just to comment yes.
And that will save the file.
So now if I run my file again which is my set the P Way it will execute the code.
You can see here this print x here print all the values from the first set which is fruits.
And this line of code three here basically is this here way is print in the length of the values in
this set called animals you can see there are three in the bed the tiger and the lion.
And that is what this value 3 represents.
So that's set for these videos.
Thanks for watching.