Practical No.
GUIDELINES FOR WRITING THE PRACTICAL WRITEUP
1. Use ruled file pages for write-up of practical contents.
2. You can use both side ruled pages for the same.
3. Start each practical (experiment) write-up on new page.
4. Maintain all these practical work in a file as it will be considered for
TW (continuous assessment) of the course PWP.
5. Write question answers given and take print out of only programs or
can write it on assignment pages.
6. Complete the practical write-up in time. Don’t keep it pending.
Practical No. 8: Write Python program to perform following operations on Set:
Create Set, Access set elements, Update set, Delete set
1. Write the various methods of set.
Ans:
Function Description
add() Adds an element to a set
Removes an element from a set. If the element
remove() is not present in the set, raise a KeyError
clear() Removes all elements form a set
copy() Returns a shallow copy of a set
Removes and returns an arbitrary set element.
pop() Raise KeyError if the set is empty
Practical No. 8
update() Updates a set with the union of itself and others
union() Returns the union of sets in a new set
Returns the difference of two or more sets as a
difference() new set
Removes all elements of another set from this
difference_update() set
Removes an element from set if it is a member.
discard() (Do nothing if the element is not in set)
Returns the intersection of two sets as a new
intersection() set
Updates the set with the intersection of itself
intersection_update() and another
isdisjoint() Returns True if two sets have a null intersection
issubset() Returns True if another set contains this set
issuperset() Returns True if this set contains another set
Returns the symmetric difference of two sets as
symmetric_difference() a new set
Practical No. 8
1. Write a python program to create a set, add members in a
set and remove one item from set
set1={1}
set1.add(3)
print("After adding 1st element ",set1)
set1.add(4)
print("After adding 2nd element ",set1)
set1.remove(4)
print("After removing random element ",set1)
Output:
>>> %Run exp8_1.py
After adding 1st element {1, 3}
After adding 2nd element {1, 3, 4}
After removing random element {1, 3}
2. Write a python program to perform following operations on
set: intersection of sets, union of sets, set difference,
symmetric difference, clear a set.
A={1,2,3,4}
B={3,4,5,6}
print("Set A is ",A)
print("Set B is ",B)
print("Intersection of sets ",A&B)
print("Union of sets ",A|B)
print("Set difference is ",A-B)
print("Symmetric difference is ",A^B)
A.clear()
B.clear()
print("Set A after clear operation ",A)
print("Set B after clear operation ",B)
Output:
>>> %Run exp8_2.py
Set A is {1, 2, 3, 4}
Set B is {3, 4, 5, 6}
Intersection of sets {3, 4}
Union of sets {1, 2, 3, 4, 5, 6}
Set difference is {1, 2}
Practical No. 8
Symmetric difference is {1, 2, 5, 6}
Set A after clear operation set()
Set B after clear operation set()
3. Write a python program to find maximum and the minimum
value in a set.
A={2,3,4,6,8,1}
B={'a','r','z','c'}
print("Maximum of set A is ",max(A))
print("Maximum of set B is ",max(B))
print()
print("Minimum of set A is ",min(A))
print("Minimum of set B is ",min(B))
Output:
>>> %Run exp8_3.py
Maximum of set A is 8
Maximum of set B is z
Minimum of set A is 1
Minimum of set B is a
4. Write a python program to find the length of a set
A={2,3,4,6,8,1}
B={'a','r','z','c'}
print("Length of set A: ",len(A))
print()
print("Length of set B: ",len(B))
Output:
>>> %Run exp8_4.py
Length of set A: 6
Length of set B: 4