Remember
frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().
frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
values = frozenset([3, 2, 2])
print(sorted(values))Top-level scope
frozenset({2, 3})frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
values = frozenset([3, 2, 2])
print(sorted(values))
|
v
DISPLAYED RESULT
[2, 3]
FINAL BINDINGS
values --> object 1 (frozenset)
object 1: frozenset({2, 3})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
values = frozenset([3, 2, 2])
print(sorted(values))Top-level scope
frozenset({2, 3})The example has finished. Compare the final output with the values and references above.
[2, 3]
What do you expect this program to print?
values = frozenset([3, 2, 2])
print(sorted(values))frozenset() FunctionThe frozenset() function in Python returns an immutable version of a set. Once created, you cannot add or remove elements from a frozenset. It is often used when you need a constant set that won’t change and can be used as a dictionary key or set element.
frozenset([iterable])
iterable – Optional. An iterable like list, set, tuple, or string. If no argument is passed, it returns an empty frozenset.nums = [1, 2, 3, 2]
fset = frozenset(nums)
print(fset)
frozenset({1, 2, 3})
Note: Duplicate elements are automatically removed, just like a normal set.
fset = frozenset("hello")
print(fset)
frozenset({'e', 'h', 'l', 'o'})
fset1 = frozenset([1, 2])
fset2 = frozenset([3, 4])
my_dict = {fset1: "Group A", fset2: "Group B"}
print(my_dict)
{frozenset({1, 2}): 'Group A', frozenset({3, 4}): 'Group B'}
Why? Because frozensets are immutable, they can be used as dictionary keys or elements of another set, unlike normal sets.
add() or remove() – will raise an AttributeError.Use frozenset when you want to store a group of items that must remain constant and be hashable (usable in keys, nested sets, etc).
frozenset() creates an immutable set.add(), remove(), or any modification methods.Create a frozenset from a tuple and try to add an element to it. What happens?
colors = ("red", "blue", "red", "green")
fcolors = frozenset(colors)
print(fcolors)
# Try this (will raise an error)
# fcolors.add("yellow")
frozenset({'green', 'blue', 'red'})
Connect what you noticed in the example with the idea below.
frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().
A frozenset keeps repeated elements.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.