hey guys welcome to the next blog on python tutorial for beginners in this blog we are going to learn how to use sets in Python so first of all what are sets so a set is an unordered collection with no duplicate elements and no indexing so let's see how we can use sets in Python so you can declare a variable and then is equal to and you define the elements in set in our curly bracket so you can define multiple values here for example 1 comma 2 comma 5 comma 4 comma 7 comma 9 but keep in mind that set cannot have duplicate values so each value in set must be a unique value so for example what happens when we define a set with some duplicate values so I'm going to just declare this set and press enter here and this set is created now whenever I try to access the values inside the set a then it's going to give me this set and you can see the two which was a duplicate which we have defined here is removed from this set so a set always have unique values and if you define a set with duplicate values it's going to remove the duplicate values and only save the unique values in the set so let's see some of the methods related to sets and the first method is as always le n method and you can find the length of a set using this le n method and the name of the set which returns the length of a set you can also use add a function so a dot add to add a element in a set so I want to add 10 for example into my set I can use this and then press Enter and once again when I try to get the values inside the set a you can see 10 is added to this set but this time will only be added if it's already not there in the set if it's already there in the set nothing will happen so let's try to add once again ten to this set which already have a set and once again you will see that nothing happens inside the set because ten was already there now if you want to add multiple values in a set you can use update methods so you can call this method which is update and then inside the curly brackets you need to provide these multiple values so let's add some values into this set so I'm going to add 15 18 17 let's say and 14 okay and then press enter and now I will see the values inside the set and you can see all these values are added to the set you can also remove the values from the set so I can use a method called remove and let's say I want to remove 18 from the set I can just give the element and then press Enter and once again I will try to access the set and you can see 18 is removed from this set now there is a method called discard also so let's see how we can use this method this card works similar to remove method so for example I want to remove this 17 from my set I can write 17 here and it works fine right so when I try to access the value of a 17 is removed so what is the difference between a discard method and a remove method so our remove method whenever you use a remove method and you try to remove an element which is not there in the set for example I will try to remove 100 which is not there in the set it's going to throw an exception and it says key error 100 now if you try to do the same thing using a discard method so I'm going to use a dot discard here and try to discard hundred from the set which is not there in the set it's not going to give me any error and that's the difference between this card and remove so remove throws an error when element is not there but this card doesn't throw any error it's not going to do anything if the value is not there in the set now there is a pop method also you can use with set so I'm I can use this pop method and then press enter and what it's going to do is it's going to remove any random element from your set so it's not necessary that is going to remove the element from the left hand side or the right hand side it will remove any random element from the set also for example I will declare a set of names let's say and if you want to clear the set you can use a clear method so I can use this clear method in order to empty this set so now when I try to access the values inside the set you can see it's an empty set with no values if you want to delete a set you can use a de L function and then the name of your set and then press Enter and once you delete it when you try to access it it's going to give you an error that name is not defined now you can also create a set using a set constructor so instead of these curly brackets you can write set and in the double parentheses so you need to provide the double parentheses here in order to create a set using the set constructor so this is also going to create this set called name and when I try to access the values inside the set name you can see it has created this set of names also you can convert a list into a set so let me define a variable called Zed and then I can use a set constructor and inside these parentheses I can use the square brackets which we use generally with lists and then you can define your list here and then press ENTER and this list will be converted to a set and you can see the result here now similar to the mathematical set operations like Union intersection difference symmetric difference you can also use these mathematical operations related to set on the Python sets also so let's see how we can use this mathematical set operations on our Python sets so let me once again define a set I have already one set which is a which contains these values for example and I will define a set B with some other set of values so I am going to define a set with for example so that is the content of the set be okay so now I have two sets and on these two sets I want to perform some set operations which are also used in mathematics so you can find out the union of two sets using operator called or okay so when I write a and this pipe symbol which is called or B it's going to give me the union of these two sets and what is the Union so union of two sets contain all the elements that are there in the set a or in the set B so or here is important right so I'm going to press ENTER and you can see it's going to give me the union of a and B that means this set contains all the elements that are there in set a or in set B also I can use a method called Union instead of this or operator so I can use a variable dot Union there is a method called Union and then you can write B here and then press ENTER which is going to give you the same answer okay so you can either use this Union method or this or operator now let's see how we can find out the intersection between two sets so in order to find out the intersection you use a and this operator end and then your next set which is B here so what is an intersection of two sets so intersection of two sets contains all the element that are there in both the set that means set a and set B okay so when I press ENTER you can see it gives me two elements inside the set and these two elements are there both in the a set and the B set that's why we get only two values because these two values are there in set a and set B also again you can use a method called intersection so a intersection and then B which is going to give me the same answer so either you can use this method or you can use this operator now let's find out the difference between two sets so what is a difference between to set a difference of two sets contains all the elements that are in a but not in B ok so you can find out the difference by this minus operator here so when you write a minus B you will be able to get the difference between these two sets and this result will contain all the elements that are in a but not in B you can also use B minus a and then is going to give you other result because this time it's going to give you a set which contains all the elements that are there in B and not in a so difference between set a minus B is totally different from B minus a and also you can use a difference method so a then you can call a difference method B and it's going to give you the same kind of answer you can see here and also you can call a B dot difference a and it's going to give you the another answer of B minus a now you can also find out the symmetric difference between two sets and what is the symmetric difference between two sets a symmetric difference between two sets contains all the elements that are either in set a but not in set B or they are there in set B but not in set E so this is the symmetric difference and you can find out the symmetric difference using this cap symbol and then B so a cap be I don't know what is the exact name of the symbol you can find out by ourselves and when you do this it is going to give you the symmetric difference between a and B and whenever you find out the symmetric difference then when you do for example be this cap symbol a it's going to give you the same answer because symmetric difference give you same answer whether you give a cap B or B cap a the answer will be always same also you can use a method symmetric difference so you can use this method called symmetric difference B and it's going to give you the same kind of result now one last thing I want to show here is sets are not indexed or ordered so whenever you want to find out for example a and the value at index 0 it's going to give you an error because there are no indexes in a set and they are not ordered by any index so sets are unordered collection of values now if you want to find out what all other methods you can use which sets you can create a set and then write the name of your set and then press dot here and you will be able to see this is the list of all the methods which you can use with the sets also if you want to list out all the methods which you can use with sets you can use this inbuilt function dir and then you can provide any set name here which is your variable name and then press Enter and once again it's going to print the list of all the method which you can use with the sets so this is how you can use sets in Python I hope you've enjoyed this blog and I will see you in the next blog.
Post a Comment