Algorithm
-
Initialize Sets:
- Create multiple sets to demonstrate set operations. Let's call them
setA
,setB
, etc.
- Create multiple sets to demonstrate set operations. Let's call them
-
Set Operations: a. Union Operation:
- Perform the union operation on sets
setA
andsetB
. - Display the result.
b. Intersection Operation:
- Perform the intersection operation on sets
setA
andsetB
. - Display the result.
c. Difference Operation:
- Perform the set difference operation (elements in
setA
but not insetB
). - Display the result.
d. Symmetric Difference Operation:
- Perform the symmetric difference operation (elements in either
setA
orsetB
, but not in both). - Display the result.
e. Subset Check:
- Check if
setB
is a subset ofsetA
. - Display the result.
f. Superset Check:
- Check if
setA
is a superset ofsetB
. - Display the result.
- Perform the union operation on sets
-
User Input (Optional):
- Optionally, you can include user input to dynamically enter elements for sets
setA
andsetB
.
- Optionally, you can include user input to dynamically enter elements for sets
-
Display Results:
- Print the results of each set operation to illustrate the different outcomes.
-
Example:
- Display the contents of sets
setA
,setB
, etc., and the results of each set operation for better understanding.
- Display the contents of sets
Code Examples
#1 Code Example- Set Union Operation
Code -
Python Programming
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set union operation
print('Union of A and B:', A | B)
Copy The Code &
Try With Live Editor
Output
#2 Code Example- Set Intersection Operation
Code -
Python Programming
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set intersection operation
print('Intersection of A and B:', A & B)
Copy The Code &
Try With Live Editor
Output
#3 Code Example- Set Difference Operation
Code -
Python Programming
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set difference operation
print('Difference of A and B:', A - B)
Copy The Code &
Try With Live Editor
Output
#4 Code Example- Set Symmetric Difference Operation
Code -
Python Programming
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set symmetric difference operation
print('Symmetric Difference of A and B:', A ^ B)
Copy The Code &
Try With Live Editor
Output
Demonstration
Python Programing Example to Illustrate Different Set Operations-DevsEnv