DEV Community

Cover image for Python Set and Frozenset
Baransel
Baransel

Posted on

Python Set and Frozenset

Sign up to my newsletter!.

What is Set and frozenset?

In our previous Data Types lesson, we divided the data types in Python into mutable (Mutable) and immutable (unchangeable) and here we will process both mutable and immutable data types.

Dictionaries are a little different because Dictionaries consist of two parts; keys and values, value part can contain all data type but keys part can only be of string and int type.

What is Set?

It is a data type that contains multiple data types such as list, dictionary, and tuple data types. You can perform all kinds of functions (joining, intersection, etc.) related to sets with this data type.

Set Usage

Let me show you the use of set with an example;

set = {"Python", 'b', 5, "Baransel"}
Enter fullscreen mode Exit fullscreen mode

It has a very simple usage, but there is something we should pay attention to here if we define an empty set as follows;

set = {}
Enter fullscreen mode Exit fullscreen mode

The interpreter will detect it as a Dictionary. Well, if you ask how to create an empty set, let me show you;

set = set()
Enter fullscreen mode Exit fullscreen mode

we define as.

Let me give another important note about sets: there can be only one element from an element, just like the sets we see here in mathematics. Well, let's see what happens if we try to add;

set = {"Python", 'b', 5, "Baransel", "Python"}

print(set)
# {"Python", 'b', 5, "Baransel"}
Enter fullscreen mode Exit fullscreen mode

Now you will say ok, but we added it, it didn't make any mistakes, but if you say it shows only one of the same ones while showing, let me show you as follows.

set = {"Python", 'b', 5, "Baransel", "Python"}
print(len(set))
# 4
Enter fullscreen mode Exit fullscreen mode

As you can see, when we look at the length of the set, the set sees 4 elements even though we add 5 elements. Since we have given this important note, let me show you how we access the elements of the Set.

Accessing Set Items

set = {"Python", 'b', 5, "Baransel", "Python"}
print(set[3])

# TypeError: 'set' object does not support indexing
Enter fullscreen mode Exit fullscreen mode

We got an error in this way because Set is not an ordered data type like Dictionaries. So it does not support indexing. Now you will say, why should I use sets, it is true, although the sets seem unnecessary on their own, you will need them when there is more than one set, then you will see how necessary the sets are. That's why I immediately switch to Set Method.

Let's show the set methods with the dir() function as in every lesson.

Set Methods

['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
 '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
 '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', 
'__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__',
 '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__',
 '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__',
 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 
'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset',
 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union',
 'update']
Enter fullscreen mode Exit fullscreen mode

In this lesson, as in other lessons, we will not go into __X__ methods, that is, special methods.

['add', 'clear', 'copy', 'difference', 'difference_update', 'discard',
 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset',
 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union',
 'update']
Enter fullscreen mode Exit fullscreen mode

add Method

Continue this post on my blog! Python Set and Frozenset.

Sign up to my newsletter!.

Top comments (0)