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 the sets. The intersection is an operation that creates a set containing common elements from both the 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 which can be performed on sets in python language too.
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:
Both of these approaches are defined in the video above.
Let me now share some basic information about sets so that you can know why they are so important and why you should learn them.
Unlike the dictionary (that we have learned in tutorial 10 and 11), sets are not just restricted to Python language, but nearly all commonly used programming languages have sets included in them as a data type. Examples of these languages include C++, Java, etc., even languages such as Swift and JavaScript support sets. One of the earliest languages that supported sets was Pascal. I hope you now have a rough idea around how important these sets actually are because whichever language you choose to code in, you must have a very basic understanding of sets!
Everything has a limit to its functionality, there are some limitations on working with sets too.
Despite these restrictions, sets play a very important 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
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.
Hope you now got a basic understanding of Sets and their working. For further explanation and understanding watch the video tutorial to grasp the concept completely. Do not forget to subscribe on YouTube :)
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))
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials
.lkmlkmlkm
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here