Introduction to sets in python:
In this tutorial we are going to learn python Set data type. So let’s first start with the definition of python set data type. It is an unordered collection of unique (non duplicates) immutable elements or items. Here an individual data item of set is called as an element or an item of set.
set_variable = {1,2,3}
In above example numbers 1, 2, 3 are known as the elements or items of set.
How to create a Set in python?
There are two ways of set creation in python.
 First way is to write all the set elements in curly braces { } with each element separated by comma.
 Second way is converting an existing list into set just by providing the existing list to python built in function set(). This built in python function converts existing list into a set.
We can create a set of any number of elements. There is no limitation on number of set elements. Also we can create set of different immutable data types together like Tuple, String, integer and float.
Let’s try to create a set using first way of set creation i.e Providing set elements manually.
set_variable = {1, 2, 3}
print(set_variable)
#Result
{1, 2, 3}
Now let’s try the second method of set creation i.e Converting the existing list of mixed data type elements into a new set.
List_variable = [2.5, 'John', 90, ('tuple_var', 22, 'Happy', 'John')]
print(set(List_variable))
#Result
{2.5, ('tuple_var', 22, 'Happy', 'John'), 'John', 90}
In above example we have converted the existing variable (List_variable) of list data type into set.
Python Set data type Vs List/Tuple data type:
Consider a case in which we want to store the data which is in list format. To store the data which is in list format we may prefer List, Tuple or Set data type. Now the question arise that, What is the main difference between these data types. Where to use List/Tuple and where to use Set?
So the major difference between List/Tuple and Set data type is that, In list or Tuple we can have multiple occurrences of same element. Whereas in case of set data type each element is unique and we can’t have multiple occurrences of same element.
Let’s see one example of set data type. Here we have created a variable of set data type named “set_variable”. Each fruit present in the list is an element of variable which is of set data type.
set_variable = {'Apple', 'Mango', 'Banana', 'Grapes', 'Mango'} print(set_variable) Result: {'Apple', 'Banana', 'Mango', 'Grapes'}
When we print this set and see the results there are few things which you must have noticed. First thing is we are having element “Mango” present twice in the list. However after printing the set, element “Mango” is printed only once. Also the second thing is sequence of printed set element is different than they are created.
The reason why multiple occurrences of element “Mango” is ignored and element sequence is changed resides in the definition of set. Definition of set tells us that, “Python set is an unordered collection of unique (Non Duplicate) immutable elements.” Which means set data type does not care about the sequence of elements and multiple occurrences of elements.
Python Set is mutable data type:
As per the definition of python Set data type, Each element must be immutable i.e We can’t create a set of element which can be changed after its creation.
Also any python data type which is mutable in nature can’t be added as a set element. That means mutable python data types like list, Dictionary or set itself can’t be added as a set element. Even if we try to add the mutable element in set it will show us an error.
Whereas if you consider a set as a whole then, Set is mutable data type i.e We can add or remove set element.
Let’s try to create a set with list data type as one of its element and see what happens.
Use of python Set data type:
 Set data type is mostly used where membership testing and elimination of duplicate entries is required. It has very powerful capability of identifying and removing duplicate entries. Which means a set can not hold multiple occurrences of same element. Due to that each element of a set is unique in nature.
 Also set’s supports mathematical operations like Union, Intersection, Difference and symmetric difference etc.
Methods available with python Set:
Set is one of the mutable python data type. That means we can edit the set elements after its creation as well. However for editing the set basic requirement is that, We should have access of set elements. Now the problem here is that, Set is an unordered collection of elements (Sequence of elements is not fixed) and due to that indexing is not supported with the sets. However there are some other inbuilt methods available, Using which it is possible to edit the sets. Lets see few of the methods available with set data type in python.
Method for adding new set elements:
 Add(): Using this method we can add single element into an existing set.
 Update(): Using this method we can add multiple elements into an existing set. Update method takes data types like tuple, string or list as an input to update into set.
#Add() method example: set_variable = {1, 2, 3} set_variable.add('New_Element') print(set_variable) #Result: {1, 2, 3, 'New_Element'} #Update() method example: List_variable = [2.5, 'John', 90, (22, 'Happy', 'John')] set_variable.update(List_variable) print(set_variable) #Result: {1, 2.5, 2, 3, 'New_Element', (22, 'Happy', 'John'), 90, 'John'}
Method for Removing/Deleting set elements:
 Discard(): This method is used to remove element from set.
 Remove(): This method is also used to remove the set element but if the input provided element is not found in set then it shows an error.
 Clear(): This method is used to clear complete set. It do not require any input since it deletes all the elements of set.
#Example of discard() method: set_variable = {1, 2, 3, 4, 'Happy', 'John'} set_variable.discard('Happy') print(set_variable) #Result: {1, 2, 3, 4, 'John'} #Example of remove() method: set_variable.remove(3) print(set_variable) #Result: {1, 2, 4, 'John'} #Example of clear() method: set_variable.clear() print(set_variable) #Result: set()
Python Set operations:
Python set data type supports mathematical operations like Union, Intersection, Difference and symmetrical difference. Sets are mostly preferred to perform these operations. There are certain inbuilt methods are available. So let’s see these methods one by one.

Python Set Union:
 This operation is used to unite two sets. It creates a new set with union of elements from both the sets. Union can be performed either using “” operator or inbuilt method “union()“. Let’s try to unite two sets “A” and “B”.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A  B)
print(A.union(B))
print(B.union(A))
#Result of all the three print statements:
{1, 2, 3, 4, 5, 6}

Python set Intersection operation:
 This operation creates a new set of common elements present between two sets. “&” operator or “intersection()” method is used to perform intersection operation on sets. Let’s continue with above sets “A” and “B” and try to perform intersection operation on it.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A & B)
print(A.intersection(B))
print(B.intersection(A))
#Result of all the three print statements:
{3, 4}

Python set Difference operation:
 Difference operation is same as subtraction operation. It created new set after subtracting one set from another. “–” operator or “difference()” method is used to perform difference operation on set.
 “AB” is a set of element that are only present in set A and not in set B. Likewise “BA” is a set of elements those are only present in set B and not in set A.
A = {1, 2, 3, 4} B = {3, 4, 5, 6} print(A  B) print(A.difference(B)) #Result of above two print statements: {1, 2} print(B  A) print(B.difference(A)) #Result of above two print statements: {5, 6}
Python set Symmetric Difference operation:
 Symmetric Difference operation creates new set of elements by eliminating common elements between them. It create new set of elements from the set A and B except common elements between them.
 To perform symmetric difference “^” operator or “symmetric_difference()” method is used.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A ^ B)
print(A.symmetric_difference(B))
#Result of above two print statements:
{1, 2, 5, 6}
Python set Membership test:
This test is used to check whether the specified item is present in the set or not. Keyword to do the membership check is “in“. Membership test gives the result in Boolean format i.e either True or False. After the test if specified item is found present in the set then it returns ‘True’ otherwise it returns ‘False’.
A = {1, 2, 3, 4} print(3 in A) #Result: True print(6 in A) #Result: False
Python Frozen set:
It is the python data type with all the characteristics of set but the only difference is that, python set is a mutable data type while python frozen set is an immutable data type. That means once the frozen set is created its elements can’t be changed.
It is same like tuple and list. We know tuple is a immutable list. likewise frozen set is an immutable set.
Keyword for creating a frozenset is “frozenset()“.
A = frozenset([1, 2, 3, 4])
print(type(A))
#Result:
<class 'frozenset'>
Continue Reading…
 Different types of operators in python.
 Type conversion and Number data type in python.
 String indexing and String slicing in python.
Also you can get more help on python forum. Click Here