Algorithm


  1. Initialize Sets:

    • Create multiple sets to demonstrate set operations. Let's call them setA, setB, etc.
  2. Set Operations: a. Union Operation:

    • Perform the union operation on sets setA and setB.
    • Display the result.

    b. Intersection Operation:

    • Perform the intersection operation on sets setA and setB.
    • Display the result.

    c. Difference Operation:

    • Perform the set difference operation (elements in setA but not in setB).
    • Display the result.

    d. Symmetric Difference Operation:

    • Perform the symmetric difference operation (elements in either setA or setB, but not in both).
    • Display the result.

    e. Subset Check:

    • Check if setB is a subset of setA.
    • Display the result.

    f. Superset Check:

    • Check if setA is a superset of setB.
    • Display the result.
  3. User Input (Optional):

    • Optionally, you can include user input to dynamically enter elements for sets setA and setB.
  4. Display Results:

    • Print the results of each set operation to illustrate the different outcomes.
  5. Example:

    • Display the contents of sets setA, setB, etc., and the results of each set operation for better understanding.

 

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

x
+
cmd
Union of A and B: {0, 1, 2, 3, 4, 5, 6, 7, 9}

#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

x
+
cmd
Intersection of A and B: {9, 2, 7}

#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

x
+
cmd
Difference of A and B: {1, 3, 5}

#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

x
+
cmd
Symmetric Difference of A and B: {0, 1, 3, 4, 5, 6}
Advertisements

Demonstration


Python Programing Example to Illustrate Different Set Operations-DevsEnv