Python Practice › Set Programs › Program 37

Create a frozenset

EasySet Programs · 37 of 48

Write a Python program to create a frozenset and show that it cannot be changed.

Advertisement
Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'

Approach

  1. frozenset(iterable); it has no add method.

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment