45.1.1. Introduction to Sets
(b) It stores only unique elements and offers various set operations.
45.1.2. Introduction to Frozenset
(a) Set does not maintain the order of insertion
45.1.3. Understanding Set Creation
#Take the elements of the list with comma separated.
a = input("data1: ")
#Use split() to convert the given input to list.
a = a.split(",")
#convert the list to set.
a = set(a)
result = sorted(a)
print("sorted set:", result)
45.2.1. Add Elements to a Set
data1 = input("data1: ")
list1 = data1.split(",")
#create a set1 from list1
set1 = set(list1)
element = input("element: ")
#add the element to the set1
set1.add(element)
print("sorted set after adding:", sorted(set1))
data2 = input("data2: ")
list2 = data2.split(",")
#update the set1 with the list2
set1.update(list2)
print("sorted set after updating:", sorted(set1))
45.2.2. Remove Elements of a Set
data1 = input("data1: ")
list1 = data1.split(",")
set1 = set(list1)
element = input("element to discard: ")
#Read the Question text carefully and complete the code...
if element in set1:
set1.discard(element)
print("sorted set after discarding:", sorted(set1))
element = input("element to remove: ")
set1.remove(element)
print("sorted set after removing:", sorted(set1))
else:
print("not in set")
45.2.3. Membership Test
data1 = input("data1: ")
list1 = data1.split(",")
my_set = set(list1)
print("sorted set:", sorted(my_set))
element = input("element: ")
if element in my_set:
print(f"is {element} in set: True")
else:
print(f"is {element} in set: False")
45.2.4. Mathematical Set Union
data1 = input("data1: ")
list1 = data1.split(",")
set1 = set(list1)
data2 = input("data2: ")
list2 = data2.split(",")
set2 = set(list2)
data3 = input("data3: ")
list3 = data3.split(",")
set3 = set(list3)
print("set1 sorted:", sorted(set1))
print("set2 sorted:", sorted(set2))
print("set3 sorted:", sorted(set3))
# set1,set2 and set3 are created, find the union of the sets
u = set1.union(set2)
print("union of set1, set2:",sorted(u))
u2 = u.union(set3)
print("union of set1, set2, set3:", sorted(u2))
i = set1 | set2
print("set1 | set2:", sorted(i))
i2 = i | set3
print("set1 | set2| set3:", sorted(i2))
45.2.5. Mathematical Set Intersection
data1 = input("data1: ")
list1 = data1.split(",")
set1 = set(list1)
data2 = input("data2: ")
list2 = data2.split(",")
set2 = set(list2)
#fill the required code below
print("set1 sorted:", sorted(set1))
print("set2 sorted:", sorted(set2))
print("Intersection:", sorted(set1.intersection(set2)))
print("sorted set after (set1 & set2):", sorted(set1&set2))
set1 &= set2
print("sorted set1 after (set1 &= set2):", sorted(set1))
print("sorted set2 after (set1 &= set2):", sorted(set2))
45.2.6. Mathematical Set Difference
data1 = input("data1: ")
list1 = data1.split(",")
set1 = set(list1)
data2 = input("data2: ")
list2 = data2.split(",")
set2 = set(list2)
#fill the required code below
print("set1 sorted:", sorted(set1))
print("set2 sorted:", sorted(set2))
print("difference using difference():", sorted(set1.difference(set2)))
set1.difference_update(set2)
print("difference using difference_update():", sorted(set1))
print("difference using (set1 - set2):", sorted(set1 - set2))
set1 -= set2
print("difference using (set1 -= set2):", sorted(set1))
45.2.7. Mathematical Set Symmetric Difference
data1 = input("data1: ")
list1 = data1.split(",")
set1 = set(list1)
data2 = input("data2: ")
list2 = data2.split(",")
set2 = set(list2)
# perform symmetric_difference
print("symmetric difference:",sorted(set1.symmetric_difference(set2)))
#perform set1^set2
print("set1 ^ set2:",sorted(set1^set2))
#perform symmetric_difference_update
set1.symmetric_difference_update(set2)
print("set1 sorted:", sorted(set1))
print("set2 sorted:", sorted(set2))
data3 = input("data3: ")
list3 = data3.split(",")
set3 = set(list3)
data4 = input("data4: ")
list4 = data4.split(",")
set4 = set(list4)
#perform set3^=set4
set3 ^= set4
print("set3 after (set3 ^= set4):", sorted(set3))
print("set4 after (set3 ^= set4):", sorted(set4))