0% found this document useful (0 votes)
8 views1 page

Set Operations in Python Explained

The document contains a Python program that demonstrates basic set operations including union, intersection, difference, and complement. It defines functions for each operation and uses a universal set along with two example sets, setA and setB. The program outputs the results of these operations when executed.

Uploaded by

sac
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Set Operations in Python Explained

The document contains a Python program that demonstrates basic set operations including union, intersection, difference, and complement. It defines functions for each operation and uses a universal set along with two example sets, setA and setB. The program outputs the results of these operations when executed.

Uploaded by

sac
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

def set_union(setA, setB):

return setA | setB

def set_intersection(setA, setB):


return setA & setB

def set_difference(setA, setB):


return setA - setB

def set_complement(universal, subset):


return universal - subset

def main():

universal = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


setA = {1, 2, 3, 4, 5}
setB = {4, 5, 6, 7, 8}

print("Universal Set:", universal)


print("Set A:", setA)
print("Set B:", setB)

print("\n--- Set Operations ---")


print("A ∪ B (Union):", set_union(setA, setB))
print("A ∩ B (Intersection):", set_intersection(setA, setB))
print("A - B (Difference):", set_difference(setA, setB))
print("B - A (Difference):", set_difference(setB, setA))
print("A' (Complement):", set_complement(universal, setA))
print("B' (Complement):", set_complement(universal, setB))

if __name__ == "__main__":
main()

You might also like