Complete the code to create an immutable set using the correct function.
my_set = [1]([1, 2, 3]) print(my_set)
set() instead of frozenset() which creates a mutable set.list() or tuple() which are not sets.The frozenset() function creates an immutable set in Python.
Complete the code to check if an element exists in a frozen set.
fset = frozenset([10, 20, 30]) if [1] in fset: print("Found") else: print("Not found")
20 is an element of the frozen set, so the condition is true and "Found" is printed.
Fix the error in the code by choosing the correct method to add an element to a frozen set.
fset = frozenset([1, 2, 3]) fset = fset.[1]([4]) print(fset)
add() or append() which do not work on frozen sets.insert() which is not a set method.Frozen sets are immutable, so you cannot use add(). Instead, use union() to create a new frozen set with the added element.
Fill both blanks to create a frozen set from a list and check if it is a subset of another frozen set.
fset1 = frozenset([1]) fset2 = frozenset([1, 2, 3, 4, 5]) print(fset1.[2](fset2))
issuperset() which checks the opposite relation.We create fset1 from a list [1, 2, 3] and check if it is a subset of fset2 using issubset().
Fill all three blanks to create a frozen set, add elements using union, and check the length.
fset = frozenset([1]) fset = fset.[2]([3]) print(len(fset))
add which does not work on frozen sets.union instead of a frozen set.We start with a frozen set of [10, 20], then use union with another frozen set frozenset([30, 40]) to add elements, and finally print the length which is 4.