1 Python Exp 14
FS20CO047
Experiment 14
Shrikrushna Dilip Patil
2 Python Exp 14
Title: Write a Python program to perform operations on
Sets.
Theory:
1. Explain set along with different operations.
A set is created by placing all the items (elements) inside curly braces
(
separated by comma, or by using the built-in set () function.
It can have any number of items and they may be of different types
(integer,
float, tuple, string etc.). But a set cannot have mutable elements like
lists, sets
or dictionaries as its elements.
A. add) Method: It adds the item x to a set if it is non-pre existing.
Example:
("AA", "BB", cc"
add ("N")
This will add NN in A set
B. union(s) Method: t returns a union of two set. Using the operator ’|’
between 2 existing sets is the same as writing
My_Set1.union(My_Set2).
Example:
A=("AA", BB, c) B-("MM,"NN”)
Z= A.union (B) OR Z AIB
3 Python Exp 14
Set population set will have components of both A and B.
C. intersect(s) Method: It returns an intersection of wo givensets . The '&’
operator can be used in this operation.
Example: s = A.intersection (B)
Set victims will contains the common element of A and B.
D. difference(s) Method: Returns a set containing all the elements which
are existing in the first set but not present in the second set. We can
use operator here.
Example:-
W=A.difference (B)
OR
SA-B
E. clear() Method: The whole existing set will become empty.
Example:
B.clear ()
Clears B set.
4 Python Exp 14
Practical:
a. Program for Create any two sets, (1,2,3,4) and (2,3,
5,4) and perform various operations on it.
Program:-
Observation:
a.
Output:-
Conclusion:
In the above experiment I learned how to use the sets in python and
perform various operations using it.