0% found this document useful (0 votes)
16 views2 pages

Unit 1 CHP 2

FrozenSet is an immutable version of a Python set. Elements cannot be modified after creation. Frozen sets can be used as keys in dictionaries or elements of another set due to immutability. Frozen sets are unordered and created using the frozenset() function, which returns an immutable set initialized from an iterable. Common set operations like union, intersection, difference, and symmetric difference can be performed on frozen sets using methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Unit 1 CHP 2

FrozenSet is an immutable version of a Python set. Elements cannot be modified after creation. Frozen sets can be used as keys in dictionaries or elements of another set due to immutability. Frozen sets are unordered and created using the frozenset() function, which returns an immutable set initialized from an iterable. Common set operations like union, intersection, difference, and symmetric difference can be performed on frozen sets using methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FrozenSet

1. Frozen set is just an immutable version of a Python set object. While elements of a set
can be modified at any time, elements of the frozen set remain the same after creation.
2. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.
3. But like sets, it is not ordered (the elements can be set at any index).
4. The syntax of frozenset() function is:
frozenset([iterable])
Iterable can be set, dictionary, tuple, etc.
5. Return value from frozenset(): The frozenset() function returns an immutable frozenset
initialized with elements from the given iterable. If no parameters are passed, it returns
an empty frozenset.
6. Example:
a) Creating frozenset using tuple
# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')
fSet = frozenset(vowels)
print('The frozen set is:', fSet)

b) Creating frozenset and then displaying is type


a=frozenset()
print('The empty frozen set is:', a)
print(type(a))

c) frozenset() for Dictionary: When you use a dictionary as an iterable for a frozen set, it
only takes keys of the dictionary to create the set.

person = {"name": "John", "age": 23, "sex": "male"}


fSet = frozenset(person)
print('The frozen set is:', fSet)

d) Trying to add elements in empty frozenset


a=frozenset()
print('The empty frozen set is:', a)
a.append(1)
#This will throw an error since frozenset is immutable
Difference between Set and FrozenSet

Set Frozen Sets


Mutable Immutable
frozenset() function is used to create empty
set() function is use to create an empty set
set
When printing a set, only the elments of the When printing element of frozenset, keyword
set is printed frozenset is also printed

Uses operator like |,^,-& for Union,


Uses keywords to perform operations eg:
Symmetric Difference, Intersection and
A.union(B)
difference respectively

Operations and methods of frozen set


1. Like normal sets, frozenset can also perform different operations like copy, difference,
intersection, symmetric_difference, and union.

2. Example:
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
# copying a frozenset
C = A.copy()
print(C)
# union
print(A.union(B))
# intersection
print(A.intersection(B))
# difference
print(A.difference(B))
# symmetric_difference
print(A.symmetric_difference(B))

You might also like