+91 81900 98289

This Frozenset lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A frozenset is a set you cannot change after it is created. It can be used as a dict key or stored inside another set.

in still works. add and remove do not exist.

Example 1: Start here

Build a new frozenset when the membership must change. You cannot edit the old one.

locked = frozenset(["Python", "SQL"]) # a set that cannot be changed print("SQL" in locked) # in is True when the value is found print(len(locked)) # len counts the items

Example 2: A set that cannot change

A frozenset supports membership tests but has no add method.

locked = frozenset(["Python", "SQL"]) # a set that cannot be changed print("Python" in locked) # in is True when the value is found

Example 3: Use it as a dict key

A normal set cannot be a dictionary key. A frozenset can, because it cannot change.

group = frozenset(["Asha", "Arun"]) # a set that cannot be changed teams = {group: "Batch 12"} # a dictionary; each key has one value print(teams[group])

Example 4: Combine two frozen sets

The result is a new frozenset. The originals stay the same.

print(frozenset(["Python"]) | frozenset(["SQL"])) # this operator works on the bits of the number
Build a new frozenset when the membership must change. You cannot edit the old one.
Call Now 81900 98289