Sem 3python Module IV Final
Sem 3python Module IV Final
List is useful for insertion and Tuple is useful for read only
deletion operations. operations like accessing elements.
Example
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print("Set 1 :",A);
print("Set 2 :",B);
print("Set of union using | :",A|B)
print("Set of union using union() :",A.union(B))
Output
Set 1 : {1, 2, 3, 4, 5}
Set 2 : {4, 5, 6, 7, 8}
Set of union using | : {1, 2, 3, 4, 5, 6, 7, 8}
Set of union using union() : {1, 2, 3, 4, 5, 6, 7, 8}
2. Intersection of Sets
The intersection operation on two sets produces a new set containing
only the common elements from both the sets. Intersection is performed
using & operator and intersection() method.
Example
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print("Set 1 :",A);
print("Set 2 :",B);
print("Set of intersection using &:",A&B)
print("Set of intersection using intersection() :",A.intersection(B))
Output
Set 1 : {1, 2, 3, 4, 5}
Set 2 : {4, 5, 6, 7, 8}
Set of intersection using &: {4, 5}
Set of intersection using intersection() : {4, 5}
3. Difference of Sets
The difference operation on two sets produces a new set containing only
the elements from the first set and none from the second set. A(A-B) is
a set of elements that are only in A but not in B. Similarly, B -A is a set
of elements in B but not in A. Difference is performed using – operator
and difference() method.
Example
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print("Set 1 :",A);
print("Set 2 :",B);
print("Set of Difference (A-B) - :",A-B)
print("Set of Difference using difference() :",A.difference(B))
print("Set of Difference (B-A) - :",B-A)
print("Set of Difference using difference() :",B.difference(A))
Output
Set 1 : {1, 2, 3, 4, 5}
Set 2 : {4, 5, 6, 7, 8}
Set of Difference (A-B) - : {1, 2, 3}
Set of Difference using difference() : {1, 2, 3}
Set of Difference (B-A) - : {8, 6, 7}
Set of Difference using difference() : {8, 6, 7}
**************