Sets in Pyhton
Sets in Pyhton
In python, a set is another type of collection data type. A Set is a mutable and an unordered collection of elements without duplicates. That means the elements within a set cannot be repeated. This feature used to include membership testing and eliminating duplicate elements.
Creating a Set
A set is created by placing all the elements separated by comma within a pair of curly brackets. The set( ) function can also used to create sets in Python.
Syntax:
Set_Variable = {E1, E2, E3 …….. En}
Example
>>> S1={1,2,3,'A',3.14} >>> print(S1) {1, 2, 3, 3.14, 'A'} >>> S2={1,2,2,'A',3.14} >>> print(S2) {1, 2, 'A', 3.14}
In the above examples, the set S1 is created with different types of elements without duplicate values. Whereas in the set S2 is created with duplicate values, but python accepts only one element among the duplications. Which means python removed the duplicate value, because a set in python cannot have duplicate elements.
Creating Set using List or Tuple
A list or Tuple can be converted as set by using set( ) function. This is very simple procedure. First you have to create a list or Tuple then, substitute its variable within set( ) function as argument.
Example
MyList=[2,4,6,8,10] MySet=set(MyList) print(MySet)
Output:
{2, 4, 6, 8, 10}
Set Operations
As you learnt in mathematics, the python is also supports the set operations such as Union, Intersection, difference and Symmetric difference.
Union: It includes all elements from two or more sets
Positive Numbers: (5, 6, 8, 3, 1)
In python, the operator | is used to union of two sets. The function union( ) is also used to join two sets in python.
Example: Program to Join (Union) two sets using union operator
set_A={2,4,6,8} set_B={'A', 'B', 'C', 'D'} U_set=set_A|set_B print(U_set)
Output:
{2, 4, 6, 8, 'A', 'D', 'C', 'B'}
Example: Program to Join (Union) two sets using union function
set_A={2,4,6,8} set_B={'A', 'B', 'C', 'D'} set_U=set_A. print(set_U)
Output:
{'D', 2, 4, 6, 8, 'B', 'C', 'A'}
(ii) Intersection: It includes the common elements in two sets
Th e operator & is used to intersect two sets in python. Th e function intersection( ) is also used to intersect two sets in python.
Example: Program to insect two sets using intersection operator
set_A={'A', 2, 4, 'D'} set_B={'A', 'B', 'C', 'D'} print(set_A & set_B)
Output:
{'A', 'D'}
Example: Program to insect two sets using intersection function
set_A={'A', 2, 4, 'D'}
set_B={'A', 'B', 'C', 'D'}
print(set_A.intersection(set_B))
Output:
{'A', 'D'}
(iii) Difference : It includes all elements that are in fi rst set (say set A) but not in the second set (say set B)
Th e minus (-) operator is used to diff erence set operation in python. Th e function difference( ) is also used to diff erence operation.
Example: Program to difference of two sets using minus operator
set_A={'A', 2, 4, 'D'} set_B={'A', 'B', 'C', 'D'} print(set_A - set_B)
Output:
{2, 4}
Example: Program to difference of two sets using difference function
set_A={'A', 2, 4, 'D'} set_B={'A', 'B', 'C', 'D'} print(set_A.diff erence(set_B))
Output:
{2, 4}
(iv) Symmetric difference : It includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.
The caret (^) operator is used to symmetric difference set operation in python. The function symmetric_difference( ) is also used to do the same operation.
Example: Program to symmetric difference of two sets using caret operator
set_A={'A', 2, 4, 'D'} set_B={'A', 'B', 'C', 'D'} print(set_A ^ set_B)
Output:
{2, 4, 'B', 'C'}
Example: Program to difference of two sets using symmetric difference function
set_A={'A', 2, 4, 'D'} set_B={'A', 'B', 'C', 'D'} print(set_A.symmetric_difference(set_B))
Output:
{2, 4, 'B', 'C'}