All Python Tutorial Links

What are sets in Python?

Sets in Python



In Python we a very useful data structure called a set which is basically a collection with no duplicates so let's say we have a list of numbers with a bunch of duplicate items like this if you want to remove the duplicates we can convert this list to a set so I'm gonna define a variable UNIX we call the set function and pass the numbers list now when we print this take a look you only have unique items so one is not repeated also note that we use curly braces to define sets so here we can define a second set using curly braces with two items one and fourth now similar to lists we can add new items to a set or remove an existing one so here we can call second that add we can append a new number here we can also call remove and we can use the Len function to get the number of items in a set so these are the basics but we're set to shine are in the powerful

mathematical operations that are supported by them let me show you so I'm going to delete these three lines these are pretty basic and renamed UNIX to first so we have two sets first and second now we can get a union of two sets using the vertical bar so this expression will return a new set that includes all the items that are either in the first or in the second set let me show you so print actually I'm gonna change this four to five so now when we run this code you can see the union of these two sets is another set that includes all the items that are either in the first set or the second set we can also get the intersection of two sets so print first and second this will return a new set that includes all the items that are in both first and second sets take a look so the only number that exists in both these sets is one we can also get the difference between two sets so print first - second when we run this code we get two three four so the first set has these additional numbers that we don't have in the second set and finally we have symmetric difference so print first carrot second this will return the items that are either in the first or second sets but not both so let's run this we get two three four five now one thing you need to know about sets is that

unlike lists their unordered collection which means the items that we have in a set are not in sequence so we cannot access them using an index in other words if we try to print 1st of 0 we will get a runtime error set object does not support indexing so if you need to access items by an index you need to use a list we'd set quite often we use one of these operations here or we can check for the existence of an item in a set so we can check to see if one is in the first set then we can print yes there you go so to recap set is an unordered

collection of unique items we cannot have duplicates and these objects are unordered they are not in sequence so we cannot access them using an index

Happy Coding :)


sets in python,python sets,python,sets,how to use sets in python,sets in python tutorial,what are sets in python,python tutorial,sets in python 3,using sets in python,python for beginners,learn python,python sets explained,python sets union,sets in python examples,sets in python length,sets python,python sets difference,python set,python programming,python sets methods,python sets tutorial,python tutorial for beginners,python lists,data types in python,data structures in python,python 3

Comments