Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

Python - Set

The document discusses the frozenset data type in Python, which is an immutable version of a set. It explains how to create frozensets using the frozenset() function and highlights their methods and use cases, particularly in situations requiring immutability. Additionally, it clarifies that while frozensets cannot be modified, they can be involved in augmented assignments that reassign the variable to a new object.

Uploaded by

sixokic135
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python - Set

The document discusses the frozenset data type in Python, which is an immutable version of a set. It explains how to create frozensets using the frozenset() function and highlights their methods and use cases, particularly in situations requiring immutability. Additionally, it clarifies that while frozensets cannot be modified, they can be involved in augmented assignments that reassign the variable to a new object.

Uploaded by

sixokic135
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python - Set

Rishav Kumar Gautam


22215214

11:11PM
enda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agenda • Agen

Tip: Use links to go to a different page inside


your presentation.

How: Highlight text, click on the link symbol Topics Covered


on the toolbar, and select the page in your
presentation you want to connect.

Frozen set Topic 2


SET
Datatype
Topic 3 Topic 4
n Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section H

Frozen set
Frozenset is a built-in type in python that has the
characteristics of a set, but its elements cannot be
changed once assigned. While tuples are immutable lists,
frozensets are immutable sets.

Back to Agenda Page


n Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section H

Frozensets can be created using the frozenset()


function.The frozenset() function returns an
immutable frozenset object initialized with
elements from the given iterable
The frozenset() function takes a single parameter:
frozenset([iterable])

Back to Agenda Page

n Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section H
# initializing A and B using frozenset function
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])

This data type supports methods like copy(), difference(), intersection(), isdisjoint(),
issubset(), issuperset(), symmetric_difference() and union().Being immutable, it does
not have methods that add remove or modify any elements.
Since a frozenset is immutable, you might think it can’t be the target of an augmented assignment
operator. But that's not totally true it can still perform some augmented assignments like:

a = frozenset([1, 2, 3, 4])
b = frozenset([3, 4, 5, 6])
a &= b
print(a)
>>>frozenset({3, 4})

Even though Python does not perform augmented assignments on frozensets in place. The statement a
&= b is effectively equivalent to a = a & b so because of this python isn’t modifying the original a. It is
just reassigning a to a new object, and the object a originally referenced is gone.
You can verify this with the id() function
Frozensets are useful in situations where you want to use a set, but you need an immutable object.

1) For example, you can’t define a set whose elements are also sets but If you really feel compelled to
define a set of sets you can do after converting the elements into a frozensets, because they are
immutable.

2) Likewise, in dictionaries we know a dictionary key must be immutable. You can’t use the built-in set
type as a dictionary key but if you find yourself needing to use sets as dictionary keys, you can use it
through frozensets:
>>>

You might also like