Python Sets are one of the four data types that are used for data collection storage. The other three data types that are used fort his purpose are lists, tuples and dictionaries. In this Python Programming Course lesson, our focus is Python Sets. We will learn the details of thiss data type of python with various coding examples.
With a set, we can store multiple items in single variable. These items in the python sets are unordered and unchangebale items. We can not change any item in s set. And we can not use duplicate items in a set. All the items must be used one time. If we use an item multiple times, then
We can use any data types in sets. This data type can be a string, a number or in any other data type. These data types can be in different set sor a one set can include different data types.
Other Python Set Lessons:
To create a python set, we use curly brackets. Inside these curly brackets, we put the items of the set. Below, you can find a set example created with string items:
We can also use set method to create a set like below:
Let’s use type method to see the type of “myset” above.
The output of this code will be:
You can practice below!
Now, let’s create different set examples with differetn data types:
With python sets, we can use different operations. Let’s give some examples to these operations.
We can combine two sets into one with the operator “|”. The same can be done with python union method.
As the output, we will see the combination of these sets. But the double items will be only once in the new set.
As a number set example, the below code will be similar:
We can find the common items in a python set with “&” operator. This can be done also with intersection method.
There are also other operations used with python sets. We will discuss them in the Python Set Operations lessons with different examples.
There are different python set methods. Let’s give some examples for this methods.
First of all let’s remember len method as we have discusses in the previous list and tuple lessons. The aim of this method is to measure the length of the set.
Here, the output of this python set code will be:
Now, let’s use this method in a string set:
The output of this code will be:
The other important method used with python sets is add method. With add method, we can add a new item to the set.
As you can see below, the new item has added to the set:
We can use another method, remoe method to remove any item in the set.
Or we can clear the items in the set with set clear method.
We can also compare two sets and print find the differences of thesse two sets with set methods. Here, to do this, we use difference method of python. In the below example, we will find the item that exist in the fist set but not in the second set.
The only item that exist in the first set but not in the second set is “Dwalin”. So the output of this python code will be:
Beside these methods, there are also other methods used with python sets. We will discuss them in python set methods lesson with different examples detailly.
In this lesson, we have learned how to use Python Sets. We have given different set examples for various usage of this data type. Generally, Python sets are very useful to find duplicate items in a set. Beside, common math operations can be done with sets like unions and intersections.