Sets In Python
Sets
In python, the set can be defined as the unordered collection of various items enclosed within
the curly braces.
The elements of the set cannot be duplicate.
The elements of the python set must be mutable.
Unlike other collections in python, there is no index attached to the elements of the set
Creating a set
The set can be created by enclosing the comma separated items with the curly braces.
Basic Set Operators
Union (|) Operator
The union of the two sets contains the all the items that are present in both the sets.
Intersection (&) Operator
The intersection of the two sets are given as the set of the elements that common in both sets.
Difference (-) Operator
The resulting set will be obtained by removing all the elements from set 1 that are present in
set 2.
Set Functions
Python contains the following methods to be used with the sets.
Those are
o len(set)
o max(set)
o min(set)
o sum(set)
o sorted(set)
o set()
o add()
o update()
o discard()
o remove()
o pop()
o clear()
o union()
o intersection()
o difference()
o issubset()
o issuperset()
Len()
In Python len() is used to find the length of set,i.e it returns the number of items in the set.
Max()
In Python max() is used to find maximum value in the set.
Min()
In Python min() is used to find minimum value in the set.
Sum()
In python, sum(set) function returns sum of all values in the set. Set values must in
number type.
Sorted()
In python, sorted (set) function is used to sort all items of set in an ascending order.
It also sorts the items into descending and ascending order.
It takes an optional parameter 'reverse' which sorts the set into descending order.
Set()
The set() method takes sequence types and converts them to sets. This is used to
convert a given string or list or tuple into set.
Add()
In python, the add() method used to add some particular item to the set.
Update()
Python provides the update () method to add more than one item in the set.
Discard()
Python provides discard () method which can be used to remove the items from the
set.
If item doesn't exist in the set, the python will not give the error. The program
maintains its control flow.
Remove()
Python provides remove () method which can be used to remove the items from the
set.
If item doesn't exist in the set, the python will give the error.
Pop()
In Python, pop () method is used to remove the item. However, this method will always
remove the last item.
Clear()
In Python, clear () method is used to remove the all items in set.
Union()
In Python, the union () method is used to perform union of two sets. The union of the
two sets contains the all the items that are present in both the sets.
Intersection()
In Python, the intersection () is used to calculate the intersection of the two sets in
python. The intersection of the two sets is given as the set of the elements that
common in both sets.
Difference()
The difference of two sets can be calculated by using the difference () method. The
resulting set will be obtained by removing all the elements from set 1 that are present
in set 2.
Issubset()
The issubset() method returns True if all elements of a set are present in another set
(passed as an argument). If not, it returns False.
Issuperset()
The issuperset () method returns True if a set has every elements of another set (passed
as an argument). If not, it returns False.