# Mathematical set operations in Python

By Abhinash Jena & Priya Chetty on September 2, 2022

The mathematical set operations can be applied to data items in Python to perform specific functions. The operators in Python for mathematical set operations are represented by special characters and keywords for different operations.

By applying mathematical set operations on sets, it is possible to compare the elements of multiple sets and even create new sets for comparisons. This article discusses mathematical set operations like Union, Intersection, Null intersection, and Intersection update operation in Python.

## Mathematical set operation Union

The union of two sets is a standard mathematical set operation to merge the elements of two sets into a single set. In sets, union operation can be applied to more than 2 sets with the syntax `set1.union(set2)` or `set1|set2|set3`.

``````a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
c = {'MatLab', 'Meta Analysis', 'Critical Review', 'SAS'}

print(a|b|c)``````
```#OUTPUT
{'SQL', 'PHP', 'Meta Analysis', 'MS Word', 'MatLab', 'HTML', 'SAS', 'R', 'SPSS', 'Python', 'Git', 'CSS', 'Critical Review', 'Tableau', 'MS Excel', 'Java'}```
``print(a.union(b).union(c))``
```#OUTPUT
{'SQL', 'PHP', 'Meta Analysis', 'MS Word', 'MatLab', 'HTML', 'SAS', 'R', 'SPSS', 'Python', 'Git', 'CSS', 'Critical Review', 'Tableau', 'MS Excel', 'Java'}```

## Mathematical set operation intersection in Python

The intersection of sets is the conjunction of sets at the common elements of the sets. The intersection of sets helps to identify the common elements in a group of sets.

``````a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
print(a & b) ``````
```#OUTPUT
{'MS Word', 'MS Excel', 'SQL'}```

Along with the `&` operator, the intersection of sets can also be performed using the syntax `set1.intersection(set2)`.

## The null intersection of sets in Python

The null intersection of sets is a conditional method to check if the sets are truly disjoint. The syntax for the method is `set1.isdisjoint(set2)` and it returns boolean values `True` or `False`.

``````a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
c = {'MatLab', 'Meta Analysis', 'Critical Review', 'SAS'}
print(a.isdisjoint(b)) ``````
```#OUTPUT
False```
``print(a.isdisjoint(c)) ``
```#OUTPUT
True```

## Intersection Update of sets in Python

The intersection update method finds the intersecting elements in the group of sets and updates the first set with the output. In simpler terms, if set A is intersection updated with B, Set A will be updated with the intersecting elements of both A & B set.

``````a = {'SPSS','Python','R','Tableau','MS Excel','MS Word','SQL'}
b = {'PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL'}
a.intersection_update(b)
print(a) ``````
```#OUTPUT
{'MS Word', 'MS Excel', 'SQL'}```
``print(b)``
```#OUTPUT
{'PHP', 'SQL', 'MS Word', 'HTML', 'Git', 'CSS', 'MS Excel', 'Java'}```

I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.

I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them.

Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here

I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal.