Hello and welcome to this session on Python and six
said is an on our door collection of data with no duplicate elements.
Until now we have talked about different sequences in python like Lis tuples dictionaries as you can
see here.
The elements in a list are the tuple or a dictionary and not unique.
So there aren't duplicate items in all of these data sequences but coming to a set it does not contain
a need duplicate element and the elements in a set of UN ordered unlike lists and tuples because sets
cannot have multiple occurrences of the same element.
It makes it highly useful to remove duplicate values from a list or a tuple next set.
I did a durable and mutable so we can add or remove elements from a set we can also perform common mathematical
operations like unions intersections symmetric differences using set.
We will have a detailed discussion on these operations in the coming up session.
You define a set containing values using curly braces like this and there can be any number of items
in a set and they may be of different types such as integers flawed tuple strings etc. But the elements
in a set can not be a mutable element like a list or another set or a dictionary.
And if you want to define an empty set in Python this is how you do.
Say for example said B is an empty set that gays you can call the in function set like this.
You do not use the curly braces but no values to define an empty set because this is resolved to define
an empty dictionary so here in the Jupiter notebook I have defined a set E and the elements in the set
a are not unique.
So let's execute this line and then display said e as you can see here.
The duplicate elements in set a.
Has been removed because multiple occurrences of an element are not allowed in a set and a set can also
be used to remove duplicate items in a tuple on a list.
So let us pass a list to the built in function set.
We'll look pass the list containing these forward values.
See that the duplicate elements have been removed from the set and in order to define an empty set you
call the built in function set like this.
And then if we check the type of set B that is the data type does it at dawn these data types at say
for example you define another set using the curly braces without any values and then execute this line
and then check the data type offsets see.
So as you can see does return the data type the because your dick empty curly braces are reserved for
defining empty dictionary and then we have also discussed that the elements in a dictionary can be of
any immutable later type such as integers flawed strings tuples but B can not have mutable elements
in a set.
Say for example I now try to change d set E..
I'll try to add in a list here.
D then execute.
So you have deed type error here.
Python has raised up type error so meaning you cannot have a mutable data type in a set.
So as we have discussed a set is created by placing all the elements inside curly braces separated by
commas or you can use the built in function set we will discuss the different methods that can be used
on set the add method can be used to add elements to a set if you want to add a single element we can
use the ad method.
So using the add method we can add immutable elements to a set.
And here we have called the add method on set E.
Let's display set a so as you can see the element ten point 0 has been added to the set and also the
set to a hill did not retain the order in which we have defined the elements because set at an unordered
collection of data if you want to remove all elements from a set you can use the clear method and then
we have the update method the Update method can take tuples less strings or other set as it argument
and this method removes any duplicate.
So let us add the elements in a list to dissect a.
So using the update methods I am gonna pass a list one to two.
One and then five common nine.
So let's execute this line and then display set a the update method as you can see has included unique
elements from the list in to the set a the list itself is not added as a single element in date set
likewise you can add elements from two different set.
So let's define another set here.
Set B contains elements such as
point eight nine dot or forty five dot o.
So if you want to add elements from say B to set a you can use the update method.
The argument should be said B.
Now letters display set a
so as you can see since all the elements and set B are unique the elements have been added to set a.
Then if you want to remove elements from a set you have the discard method and the remove method.
So using the discard method I'm going to remove one of the elements here.
So I'm going to remove this element and then displace it a say for example the element that you want
to discard from this set is not existing in on set is since a dot eight is removed from the set.
Let's try to discard this element again and as you can see there is no exception raised when you tried
to discard an element which is not existing in a set but coming to the remove method if we tried to
remove the same element a dot aid from said a it is going to raise an exception key error.
And then you also had the pop metal that can be used to remove an arbitrary element from a set so this
element has been removed from the set.
Let's now display the set E.
So as you can see the pop method removes a random item from from a set and then you can use the Lent
function to calculate the length of a set.
So since there are 10 elements the lend function has returned 10 and we have discussed before that a
common use of set and python is computing standard mathematical operations such as union intersection
diffidence and symmetric difference.
So let's discuss about these methods in detail.
So here I have defined to set set C containing the element one two three four and five and then said
D contains the elements two four six eight and 10.
Now I want to find out all of the values which are existing in set C and set D.
So I want to find out all the unique values in these two set.
So that means I want to retrieve these values in this area.
In order to retrieve these values I can use the union method.
Set C union said D.
And this is going to retrieve
one two three four five six eight and 10.
So here in my Jupiter notebook I have defined these two sets set C and set D.
Now I want to find out all the unique values from these two sets so me will use the union method to
retrieve all the unique values from the set.
So I'm going to pass set D as an argument to the union matter and then execute.
As you can see here the union method has returned a set of all of the values that are existing in set
C or said D or both.
Next if you want to find out the elements which are common to set C and said D.
So I'm talking about the values in this area in order to get these values we can use the intersection
method so set C dot intersection and we are going to pass the set D as the argument to this method and
then execute.
So these two elements to unfold add the common elements in these two sets.
Next if I want to retrieve the elements in set C which are not existing in set D that means I want to
retrieve the values in this area.
So in that case I can use the different method in order to the G d values 1 3 and 5 the elements 2 and
4 add existing instead D.
So I do not want to retrieve those values
so said see Daud diffident is the method you pass.
D said D as the argument and this is going to retrieve all of the elements in said C which are not part
of said D.
So as you can see these elements 1 3 5 are unique to set C.
Next say for example I want to retrieve all of the elements that are by use of exactly one of the two
sets but not both.
So I want to retrieve all the elements in this area and this area as well.
So I want to retrieve all these six elements.
And because 2 and 4 are common to set C and set D I do not want to retrieve these two elements.
So if I want to retrieve the values that are existing in one of the two sets but not bored I can use
the method symmetric difference.
So let's call this method symmetric difference so when I execute this say I'm going to retrieve the
elements that are existing in one of the two sets but not put the next method that we're going to talk
about is the is disjoint method.
And this method returns true if the two sets have a null intersection that means both of the sets do
not have any element in common.
So if we call the method is does join and pass these said these is a argument.
This is going to return false.
That's because there are some common elements between these two sets set C and set D.
No let's display said B.
Let us now see is disjoint method returns then we pass said B as an argument.
So said C Dart is disjoint set B.
So this method does return true because there are no common elements between set C and set B
and the next method is subset.
But you don't draw if another set contains this set.
So here I have called the is subset method on set C by passing said D as an argument and this is returned
false because not all of the elements in set C and existing in set D similarly B have the is super set
which returns true if this particular set contains another set.
So I'm gonna call inequality is super set method on set C and I have passed a set containing these two
elements 1 and 2 to this particular method.
And since these elements are existing and set C this method has returned true.
And then we can also perform membership test to check whether a specific element is contained in a set.
So for example I want to check if one is existing in Set C in Set C so because the element 1 is existing
in said see this as return true.
Let's say 1 not in Set C so they should return false.
Next we have the sorted function that can be used to sort the elements in a set.
So we have talked about set and set an unordered collection of data.
But if you need to get these values from your set in an ordered form you can use the sorted function
which outputs a list that is ordered.
So here I have defined set e containing these elements and they add in an unsorted audit.
So if you want to saw the order of the element in Set A.
We can use these altered function so we will look past set into this function and this function sorted
is going to return these elements in ascending order.
And this brings us to the end of the discussion on the method that can be used on sets.
You can either it through a set just like we do for other data sequences such as lists dictionary and
tuples so we can use a for loop to retrieve all of the elements in a set so here I have defined a set
said each containing different elements.
Now using a for loop will print all of the elements in set E..
So for x in set e meaning for each element and set e print the element so print x.
So as you can see here the order in which these elements have been printed using the for loop is different
from the order in which we have defined set E.
That's because the element in a set are in an on ordered form.