Setting Off The Sets
A Theory That Changed Practicality Of Mathematics

Hey everyone, I hope you all are doing well and learning relentlessly. Let’s revisit the house of our snake, which we discussed in the introduction of data structures. Our snake put all his scattered stuff together, and his mom is happy. He put all the books on the bookshelf and all the toys in the toy box. The snake has one bookshelf in his house, and he put all the books on it without any category. For example, he put mathematics books and manga both on the top rack of the bookshelf.
One day, the snake wants to study probability in mathematics. He goes to his bookshelf and starts searching for the book on probability, but instead, he finds his manga and gets distracted, forgetting to learn probability and wasting his whole time on manga.
His mom is angry again, and she suggests that he should group the same category of books together and place them on separate racks of the bookshelf. This way, our snake will not distract himself and can easily find math books before mangas. Remember the grouping of elements (here we can say books).

Mathematical Intuition For Grouping
Imagine we are in the world of mathematics, where there are numbers (by numbers, I mean any number, fractions, negative or positive) scattered here and there. Not only numbers, but after traveling a few meters, you also see some functions ‘f(x)’ scattered around as well. Some are positive functions, and some are quadratic functions all together.
There is a mathematician (our genius snake) who wants to calculate some values using a function. Let’s suppose ‘f(x) = x*x’ is the function whose value he/she wants to find. As we can see, f(x) is a quadratic function.

But, there is one more condition and that is, he can only use positive whole numbers nothing else as an input ‘x’ to find f(x) , for clarity see the image below..

Now imagine our world where he is living..where all types of numbers are scattered here and there. How will he find all the positive numbers from infinte numbers of all the types (negative, fraction, etc.) ?
The answer is, he will first group the required numbers together; otherwise, he might get confused about which numbers he has already picked and which are left. By creating a group, he knows where to pick an input value for our function f(x) = x*x. This makes it quicker for him to find the result of the function f(x).

What are Sets ? (Just a Group)
As we saw above, our genius snake grouped similar data together. This group is infact known as set.
Sets are collections of distinct objects, these objects can be anything from numbers, alphabets to people, anything you can think of as a collection comes under sets. Sets are represented by ‘{}’.

From above image, we can write our sets as..
Set of Positive Integers = {1, 2, 3, 4, 5, 6, 8, 10, 11, 20, 50, 100}
Set of People = {Indian, Britishers, Australians, Africans, Americans}
Sets can have differt type of objects inside it. But in formal mathematcs , mathematicians restrict sets to have similar types of objects. This makes opearions (like, finding the value of above function with condition) easy to do.
Elements → The objects inside the sets are known as elements of that set.

Formally in mathematics, we use Z to represent a set of all the integers (Positive and Negative), and R for group all real numbers together i.e set of real numbers.
Sets can be finite and infinite both. Set Theory is all about understanding how groups of things relate to each other. We will also discuss set operations, which help us study how groups relate to each other.
There are some more important terminologies for a set let’s discuss them…
Subset
Let’s suppose there are two sets A and B, then A is subset of B if every element of A is also in B. It doesn’t matter if B has more element then A but it must contain all the elements present in A.
Superset
In above example, B is our superset of A because it contain all the elements present in A. Overall, if A is subset of B then B is superset of A.
Disjoint
If two sets have no common elements then those sets are disjoints. In above image set of people and set of positive integers are disjoint sets because nothing is common between them.
Why Sets?
As we discussed in the stories above, grouping distinct objects makes it very easy to perform operations on data and understand it properly. Especially in the context of machine learning, we should understand how data behaves and what patterns it shows. Grouping them correctly helps us understand the patterns in a better and more efficient way.
Sets play a huge role in probability and statistics because we need to work with data in these fields of mathematics.
Sets Operations (How Two Groups Related To Each Other ?)
We know what sets are and why we use sets. But how exactly sets help us to find some kind of relationship between two groups ? To answer this question, mathematicians develop some operations specially for sets. We perform these operations on sets and can easily understand the relationship between two sets. Let’s discuss each operation in detail…
Union (∪)
Union simlply means combining distict elements of two different sets (groups) together. It is mainly used to gather all the possibilities from the data present in two sets. For example, let’s say s1 is the set of players who play cricket and s2 is the set of players who play cricket and we want to know how many play either cricket or football or both then we will use union.
It is represented by ‘∪’. If A∪B is written, that means A union B. See below for clarity….


Intersection (∩)
Intersection helps us detect the common elements present in two or more sets. It is mainly used when we want to know what is common between two groups. For example, if there are two sets:
s1 → {People who play football}
s2 → {People who play cricket}
And we want to know the number of people who play both football and cricket, then we can use the intersection between s1 and s2.


Difference(- or /)
It is not algebric difference . If set1 - set2 is written then it simply mean give elements present in set1 but not in set2 as a result. Similarly, if set2 - set1 is written then it simply means give element present in set2 but not in set1. See the image below for clarity…


Symmetric Difference (Δ)
To put it simply, it is used to delete the common element present in both the sets.
See the images for clarity…


Complement (‘)
Before learning about the complement, we should understand what a universal set is. A universal set, represented by U, contains all the elements within a particular context. For example, the universal set for integers is Z, which contains all integers up to infinity. The universal set of Hindi movies would be the Bollywood set (Bollywood is a universal set in the context of Hindi movies).
The complement of set A is the elements not in set A relative to the universal set U. See the image for more clarity.

Applications of Sets
Sets have many applications but some of the important ones are..
Help to study mathematics as a group of different numbers and functions make it less confusing for mathematicians.
Database Queries are also executed using set operations (UNION, INTERSECT, EXCEPT are set based operations)
Its main application is to deal with unique data especially in machine learning we have very noisy data but it help to get only unique data from that noisy data and delete the duplicates which help engineers to develop a fast and efficient machine learning model.
It also play a pivotal role in the field of probability. It helps us to understand events properly which is the base for the whole machine learning and Artificial Intelligence.
Sets In Python Programming
Till now we discussed what are sets, why we use them and how we use them but in mathematical language. Let’s see how we create and use sets and its operation in Python language.
Creating A Set In Python
In Python, we use ‘{element1, element2,……}’ to define a set, just like we do in mathematics. In mathematics, just {} without elements inside it is called an empty set.
To define an empty set in Python, we must call the set() function. If we try to define an empty set just by using ‘{}’, Python will create an empty dictionary instead of an empty set. See the image for clarity.

But if we put elements inside ‘{}’ without key-value pairs, Python will count it as a set. Remember, a set must contain immutable objects as elements; mutable objects are not allowed inside a set. (To learn about mutable and immutable objects, read this article). See the image for more clarity…

Set’s Uniqueness In Python
As the definition of a set in mathematics tells us, it is a collection of distinct objects. This means each object value can occur only once in a particular set. Hence, no duplicates are allowed in sets.
This fact is also true about sets in Python. Python doesn’t allow duplicate values inside a set. We can assign duplicate values to a set, but when we print it, Python will output it after removing the duplicates present in the set. See the image for clarity…

Set Operations In Python
Adding An Element (add())
To add an element in a set we use add() method in python. It takes one argument i.e, the element we want to add in our set.
A = {1, 2, 3, 4} B = {5, 6, 7, 8} A.add(5) # Adding single element in set A print(A) #Output -> {1, 2, 3, 4, 5}Adding Multiple Elements(update())
To add multiple elements in a set we use update() method. It take one argument, but that argument must be iterable such as lists, or other set, etc.
A = {1, 2, 3, 4} B = {5, 6, 7, 8} A.update([6, 7, 8, 9, 10]) # Adding multiple elements print(A) #Output -> {1, 2, 3, 4, 6, 7, 8, 9, 10}Removing A Specific Element(remove() and discard())
To remove a specific element from our set, we use the remove() and discard() methods. They both take the element we want to remove as an argument.
The difference between them is if we use remove() and the element is not found inside our set, it will cause an error. But if we use discard() and the element is not found inside our set, it will give no errors and run our program smoothly. They both do not return the removed element.

Removing An Arbitrary Element(pop())
To remove and return a random from our set we use pop() method. It doesn’t take any argument. It just randomly remove an element from our set and return it as output.
A = {1, 2, 3, 4, 5} removed_element = A.pop() #Storing the returned element (pop method return the removed element) print(removed_element) #Output -> 1Deleting All Elements(clear())
To remove all the elements from the set and make it empty set we use clear() method in Python. It doesn’t take any argument.

Make A Copy(copy())
To store a copy of one set to another set we use copy() method.

Union(| or union())
It is similar to the union of sets in mathematics. This means combining all the elements from two or more sets without duplication. We can also use the ‘|’ operator to perform a union. In the program below, I use both methods to perform a union, and the output is the same.

Intersection(& or intersection())
It is also similar to the intersection of sets in mathematics. It simply gives the common element present in two or more set as output. We can also use & operator to perform intersection between sets.

Difference(- or difference())
If there are two sets, A and B, and we take their difference A-B, it will return the elements present in A but not in B. It is the same as the difference of sets discussed in mathematics. We can use the ‘-’ operator or the difference() method to perform this operation.

Symmetric Difference(^ or symmetric_difference())
It will return elements that are present either in A or B but not in both. We can use the ‘^’ operator or the symmetric_difference() method to perform this operation on sets.

Check For Subset(issubset())
To check if one set is subset of other we use issubset() method in Python. It returns a boolean value i.e., True or False.

Check For Superset(issuperset())
To check if one set is superset of other we use issuperset() method in Python. It also returns a True or False.

Check For Disjoint(isdisjoint())
To check if two sets are disjoint or not we use isdisjoint() method in Python. It return True or False as a output.

Conclusion
This article explores the concept of sets, both in mathematics and programming, using a storytelling approach. It delves into how sets are used to organize data, making it easier to find relationships and patterns, especially in fields like probability, statistics, and machine learning. The article also covers set operations such as union, intersection, difference, and complements, and discusses their applications in Python programming. Additionally, it explains key concepts like subsets, supersets, and disjoint sets, demonstrating their usage and importance in data handling and analysis.
Thanks for reading such a lengthy article. Always open for feedback give me some. For more articles cliks here .
Special thanks to the #ChaiCode team for such challenges where I can express myself through articles.



