Member-only story
Hi everyone, welcome back. In this tutorial, I will show how to perform basic tasks involving sets in Python. A set is a data structure in the Python language and it is capable of storing several items in a singular set variable. Sets are a built in data type in Python. We will go over set related functions and how sets can be manipulated in this tutorial.
Sets
We can create an empty set by using curly brackets:
set = {}
We can create a set and populate it by adding values within the curly brackets:
set = {"item1", "item2", "item3"}
As you can see, I added three string items into my set and each item in my set is separated by a comma. We can print our set to see what it looks like:
set = {"item1", "item2", "item3"}
print(set)Output
{'item3', 'item2', 'item1'}
Python has 4 different basic built in data structures. They are lists, tuples, dictionaries, and sets. Each one has different uses for different situations. Sets are immutable meaning once the object is created, its values cannot be changed, but items can still be added or removed. Sets are also unordered, notice how the above output is not printed in the order it was added. The order in which the items appear…