Python Lab Manual Answers (22616)MSBTE | Practical 8 |

Python Lab Manual Answers (22616) MSBTE 

Practical 8: Write Python program to perform following operations on 
Set: Create Set, Access Set elements, Update Set, Delete Set



X. Practical related Questions

1. Describe the various set operations
Ans=>1) Set is collection of objects set are unordered. we can't access elements of sets by index. We Can perform different operations on sets below we have mention them
           2) Intersection: This operation returns elements of sets which presents in both sets.
A={0,2,4,6,8};
B={1,2,3,4,5};
print("Intersection : ",A&B)
 
        2) Difference : this operation returns set of all elements in first  set that are not in second set
A={0,2,4,6,8};
B={1,2,3,4,5};
print("Difference   : ",A-B)
 
       
     3) Union : This operation returns set of all elements that present in both sets but avoid duplicates
A={0,2,4,6,8};
B={1,2,3,4,5};
print("Union        : ",A|B)
 
       

2. Describe the various methods of set
Ans=> 1) add() : Add An element to the set.
            2) copy() : Return copy of set.
            3) discard() : Remove specified element from set.
            4) issubset() : Check that whether given set  is subset of second set.
            5) pop () : Removes element from the set

.

XI. Exercise

1. Write a Python program to create a set, add member(s) in a set and remove one 
item from set.
Ans=>
set1={0,2,4,6,8}
set1.add(10)
set1.add(12)
print("After Addition ",set1)
set1.remove(2)
print("After Deletion ",set1)
        

2. Write a Python program to perform following operations on set: intersection of sets, union of sets, set difference, symmetric difference, clear a set.
Ans=>
A={0,2,4,6,8};
B={1,2,3,4,5};
print("Union        : ",A|B)
print("Intersection : ",A&B)
print("Difference   : ",A-B)

     




3. Write a Python program to find maximum and the minimum value in a set.
Ans=>
set1={0,1,2,3,4,5,6,7,8}
print("Maximum Value : ",max(set1))
print("Manimum Value : ",min(set1))



4. Write a Python program to find the length of a set.
Ans=>

set1={0,2,4,6,8,10}
print("Length of set : ",len(set1))
     
  Hello Guys if you find it helpful then dont forget to share with your friends...
.
.
.
.
.
keep Growing❤

Post a Comment

0 Comments