Python Practice › Set Programs › Program 37
Create a frozenset
Write a Python program to create a frozenset and show that it cannot be changed.
Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'Approach
frozenset(iterable); it has noaddmethod.
Solution
fs = frozenset([3, 1, 2])print(fs)try: fs.add(4)except AttributeError as e: print("Error:", e)Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'Learn the concept
New to this topic? Read the lesson, then come back.