MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python sets


Let's start with the basic definition of sets in Mathematics. People already familiar with the concept of sets in mathematics know that as per the mathematical definition - A set is a collection of well-defined objects and non-repetitive elements that is - a set with 1,2,3,4,3,4,5,2, 2, and 3 as its elements can be written as {1,2,3,4,5}

No repetition of elements is allowed in sets.

In Python programming, sets are more or less the same. Let's look at the Python programming definition of sets:



“A set is a data structure, having unordered, unique, and unindexed elements.”

Elements in a set are also called entries, and no two entries could be the same within a set.

If your curiosity isn’t satisfied with the basic definition, do not worry, as we are approaching a more formal illustrative approach to an understanding of python sets.

Well, now that you have a basic idea about sets in mathematics. Let me tell you that a mathematical set has some basic operations which can be performed on them. For example, the union of two sets is a set made using all the elements from both sets. The intersection is an operation that creates a set containing common elements from both sets. A python set has all the properties and attributes of the mathematical set. The union, intersection, disjoint, etc., all methods are exactly the same and can be performed on sets in python language.

Note: If you are a programming beginner who doesn't know much about sets in mathematics. You can simply understand that sets in python are data types containing unique elements.

If you want to use sets in your python programs, you should know the following properties of sets in Python:


Sets are iterable(iterations can be performed using loops)
They are mutable (can be updated by adding or removing entries)
There is no duplication (two same entries do not occur)

Structure:

Elements of the sets are written in between two curly brackets and are separated with a comma, and in this simple way, we can create a set in Python.
The other way of forming a set is by using a built-in set constructor function.

Restrictions:

Everything has a limit to its functionality, and there are some limitations on working with sets too.

Once a set is created, you can not change any of its items. Although you can add new items or remove previous but updating an already existing item is not possible.
There is no indexing in sets, so accessing an item in order or through a key is not possible, although we can ask the program if the specific keyword we are looking for is present in the set by using the “in” keyword or by looping through the set by using a for loop(we will cover for loops in tutorial # 16 and 17)
Despite these restrictions, sets play a crucial role in the life of a python programmer. In most cases, these restrictions are never a problem for the programmer, given he knows which data type to use when. And this skill is something you will learn with time after writing a lot of python programs.

Set Methods:

There are already a lot of built-in methods that you can use for your ease, and they are easily accessible through the internet. You might want to peep into python's official documentation at times as well to check for some updates they might push down the line. Some of the methods you can use with sets include union(), discard(), add(), isdisjoint(), etc., and their functionality is the same as in the sets in mathematics. Also, the purpose of these functions can easily be understood by their names.

Example


s = set()
# print(type(s))
# l = [1, 2, 3, 4]
# s_from_list = set(l)
# print(s_from_list)
# print(type(s_from_list))
s.add(1)
s.add(2)
s.remove(2)
s1 = {4, 6}
print(s.isdisjoint(s1))

Output

True


Conclusion

In this page (written and validated by ) you learned about Python sets . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python If Else and Elif Conditional.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.