Set Concepts in Python

By
What are sets
  • A python set is the collection of unordered elements.Hence order of insertion is not same as the order of access. 
e.g.
set1=set() #create an empty set
set1={10,20,30,40,50}
print(set1)
Output:-
{40, 10, 50, 20, 30}
>>> 
  • A set does not contain duplicate items like list and tuple.
e.g.
#set1 contains duplicate items
set1={10,20,20,10,50,60,60,70,80,30,50}
print(set1)
Output:-
{70, 10, 80, 50, 20, 60, 30}
>>> 
  • A set is mutable (changeable),Means we can change the items after created but set items must be immutable as shown in below examples.
e.g.
#creating set with immutable items
set1={10,20,'ram','sita',2.3}
print(type(set1))

#creating set with immutable items
set2={10,20,'ram',('MNT LAB',1,2,3,4,5)}
print(type(set2))

#creating set with mutable items
set3={10,20,'ram',['MNT LAB',1,2,3,4,5]}
print(type(set3))
Output:-
<class 'set'>
<class 'set'>
Traceback (most recent call last):
  File "C:/Users/RAMASHANKER VERMA/Desktop/set.py", line 10, in <module>
    set3={10,20,'ram',['MNT LAB',1,2,3,4,5]}
TypeError: unhashable type: 'list'
>>> 
Descriptions:-In above example ,I have used list inside the set3 but lists are mutable that's why set3 gives error. In set2 , i have used tuple inside the set2 but tuples are immutable that's why set2 does not give error.After these examples i have found that sets are mutable but items in sets must be immutable.
  • Set() function can be used to convert  the  string ,list and tuple into set.While creating a set using set() method,repetitions of items are eliminated.
e.g.
#Convert the list ,tuple and string to set using set() function
list1=[10,20,30,'ram','mohan']
tuple1=(50,60,70,80,"lion")
str1="Welcome to MNT LAB"
s1=set(list1)
s2=set(tuple1)
s3=set(str1)
print(s1)
print(s2)
print(s3)
Output:-
{10, 20, 'mohan', 'ram', 30}
{70, 'lion', 80, 50, 60}
{'T', 'L', 'c', 'l', 'm', ' ', 't', 'o', 'W', 'N', 'A', 'B', 'e', 'M'}
>>> 
  • A set can not contain a set embedded in it.
e.g.
set1={10,20,30,'ram','mohan'}
set2={50,60,set1,70,80,"lion"}
print(set2) 
Output:- 
Traceback (most recent call last): File "C:/Users/RAMASHANKER VERMA/Desktop/set.py", line 3, in <module> set2={50,60,set1,70,80,"lion"} TypeError: unhashable type: 'set' 
>>> 
  • A set can be created by enclosing the comma-separated immutable items with the curly braces{}.
e.g.
set1={10,20,30,'ram','mohan',50,60,70,80,"lion"}
set2={1,2,3,(4,5,6,7,'ram'),'mohan'}
print(set1)
print(set2)
Output:-{70, 'mohan', 10, 'lion', 80, 50, 20, 60, 'ram', 30} {1, 2, 3, 'mohan', (4, 5, 6, 7, 'ram')}
>>>  
Program 1: Write a program to print the sets items using for loop.
set1={10,20,30,'ram','mohan',50,60,70,80,"lion"}
print(set1)
print("printing the set items using for loop...")
for i in set1:
    print(i)
Output:-
{70, 'lion', 10, 'mohan', 80, 50, 20, 'ram', 60, 30}
printing the set items using for loop...
70
lion
10
mohan
80
50
20
ram
60
30
>>> 

Accessing the set elements
  • Due to an unordered collection,items in set can not be accessed using indices.
  • The Sets can not sliced using[].
  • Entire set can be printed by the name of the set.
e.g.
set1={10,20,30,'ram',"lion"}
print(set1)
Output:-
{10, 'ram', 20, 'lion', 30}
>>> 
  • The sets can be iterated over using a for loop like strings,lists and tuples.
e.g.
set1={10,20,30,40,50,'ram',"lion"}
for i in set1:
      print(i)
Output:-
40
10
50
20
lion
ram
30
>>> 
Set Operations
There are some built-in functions and common set operations as shown below:-
  • Create a set
e.g.
s1=set()# create empty set
print(s1)
s2={10,20,'ram',"lion"} # create set with items
print(s2)
Output:-
()
{'ram', 10, 20, 'lion'}
>>> 
  • Use Membership operators in set as shown below.
e.g.
s1={10,20,'ram',"lion",'m',"MNT LAB"} #set with items
a= 'm' in s1
b= "MNT LAB" in s1
c=20 not in s1
print(a)
print(b)
print(c)
Output:-
True
True
False
>>>
  • len() function is used to return number of items in set as shown below.
e.g.
s1={10,20,'ram',"lion",'m',"MNT LAB"} #set with items
a=len(s1)
print(a)
Output:-
6
>>> 
  • max() function is used to return the maximum element in the set.
e.g.
s1={10,20,30,40,60,79,80,45,69,99} #set with items
a=max(s1)
print(a)
Output:-
99
>>>
  • min() function is used to return the minimum element in the set.
e.g.
s1={10,20,30,40,60,79,80,45,69,99} #set with items
a=min(s1)
print(a)
Output:-
10
>>> 
  • sorted() function is used to return the sorted set but set elements remains unchanged.
e.g.
s1={70,40,30,80,54,60,65,60} 
a=sorted(s1)
print(a)
Output:-
[30, 40, 54, 60, 65, 70, 80]
>>> 
  • This is possible to unpack a set using * operator.
e.g.
s1={70,40,30,80,54,60,65,60}
s2={10,20,30,*s1,50}
print(s1)
print(s2)
Output:-
{65, 70, 40, 80, 54, 60, 30}
{65, 70, 40, 10, 80, 50, 20, 54, 60, 30}
>>> 
Set Functions
  • add() function is used to insert single item to the set at a time.Means this function takes one argument at a time.
e.g.
s1={10,20,30,50}
print(s1)
s1.add("ram")
print(s1)
s1.add(90)
print(s1)
Output:-
{10, 20, 50, 30}
{10, 'ram', 50, 20, 30}
{10, 'ram', 50, 20, 90, 30}
>>> 
  • update() function is used to update the multiple items to the set.
e.g.
set1={10,20,30,50}
set2={60,70,80,90,100}
set1.update(set2)
print(set1)
set1.update({120,140,150,250})
print(set1)
Output:-
{100, 70, 10, 80, 50, 20, 90, 60, 30}
{100, 70, 10, 250, 140, 80, 50, 20, 150, 120, 90, 60, 30}
>>> 
  • remove() function is used to remove the items from the set. If the item does not exist in the set then remove() function will through an error.
e.g.
set1={10,20,30,50,"ram",'mahesh'}
print(set1)
set1.remove("ram")
print(set1)
set1.remove(80) #item is not present in set1.
print(set1)
Output:-
{10, 'mahesh', 50, 20, 'ram', 30}
{10, 'mahesh', 50, 20, 30}
Traceback (most recent call last):
  File "C:\Users\RAMASHANKER VERMA\Desktop\set.py", line 5, in <module>
    set1.remove(80) #item is not present in set1.
KeyError: 80
>>> 
  • discard() function  is used to remove the items from the set.If the item does not exist in the set then set remains unchanged.But there is no error found like remove() function.
e.g.
set1={10,20,30,50,"ram",'mahesh'}
print(set1)
set1.discard("ram")
print(set1)
set1.discard(80) #item is not present in set1.
print(set1)
Output:-
{'ram', 10, 50, 20, 'mahesh', 30}
{10, 50, 20, 'mahesh', 30}
{10, 50, 20, 'mahesh', 30}
>>> 
  • clear() function is used to remove all items from set.
e.g.
set1={10,20,30,50,"ram",'mahesh'}
set1.clear()
print(set1)
Output:-
set()
>>>
  • pop() function is used to remove the item.pop() method will always remove the last item from the set but set is unordered.We can not determine which element will be popped from the set.
e.g.
set1={10,20,30,50,60,80,"ram",'mahesh'}
set1.pop()
print(set1)
set1.pop()
set1.pop()
print(set1)
Output:-
{10, 80, 50, 20, 60, 'ram', 30}
{50, 20, 60, 'ram', 30}
>>> 
Mathematical set operations
There are some mathematical set operations used in python as given below:-
  • Union of two sets:-The union of two sets is calculated by using the pipe(|) operator. The union of the two sets contains all the items that are available in both the sets.
e.g.
set1={10,20,30,50,"ram",'mahesh'}
set2={20,'ram','mohan'}
a=set1|set2
print(a)
Output:-
{'mohan', 10, 'mahesh', 50, 20, 'ram', 30}
>>> 
  • Union():-This used to calculate the union of two sets.
e.g.
set1={10,20,30,50,"ram",'mahesh'}
set2={20,'ram','mohan'}
a=set1.union(set2)
print(a)
Output:-
{'mahesh', 10, 'mohan', 50, 20, 'ram', 30}
>>>
  • Intersection of two sets:-The intersection of two sets is performed by using &(and) operator.The intersection of the two sets are given as the set of elements that common in both set.
e.g.
set1={1,2,3,4,5,6,7,8}
set2={7,9,3,5,11,23,45,90}
a=set1&set2
print(a)
Output:-
{3, 5, 7}
>>> 
  • intersection() :-This method is used to calculate the common element between two sets.
e.g.
set1={'a','b''c','d','e','f','g'}
set2={'e','d','f','c','h','i','a'}
a=set1.intersection(set2)
print(a)
Output:-
{'e', 'd', 'f', 'a'}
>>> 
Updating Set Operations:-
The mathematical set operations can be extended to update an existing set.
  • x|=y :- Update x with the result of x|y.
  • x&=y :-Update x with the result of x&y. 
  • x-=y :-Update x with the result of x-y. 
  • x^=y :-Update x with the result of x^y. 
e.g.
x={1,2,3,4,5,6,7,8}
y={7,9,3,5,11,23,45,90}
#x=x|y
#x=x&y
#x=x^y
x=x-y
print(x)
Output:-
{1, 2, 4, 6, 8}
>>> 
Watch Complete Lecture Video

For More...

0 comments:

Post a Comment

Powered by Blogger.