Set Operations in Python – PythonForBeginners.com

0
45
Set Comprehension in Python - PythonForBeginners.com


Sets are container objects that contain unique elements in it. In this article, we will look at various set operations like union, intersection, and set difference. We will also implement the set operations in python.

Union Operation

If we are given two sets A and B, the union of both sets is calculated as a set that contains elements from both the set A and set B. We denote a set union operation using a   sign.

If we have a set C that is union of A and B, we can write C= A B.  For example, suppose that we have set A={1, 2, 3, 4, 5 } and set B = {2, 4, 6,8},  then the set C= A B will contain elements {1, 2, 3, 4, 5, 6, 8 }. You can observe that each element in C either belongs to A or belongs to B or belongs to both A and B. 

We can implement the set union operation in python using the union() method. The union() method when invoked on a set A takes another set B as input argument and returns the set formed by union of A and B. This can be observed as follows.

A = {1, 2, 3, 4, 5}
B = {2, 4, 6, 8}
print("Set A is:", A)
print("Set B is:", B)
C = A.union(B)
print("Union of A and B is:", C)

Output:

Set A is: {1, 2, 3, 4, 5}
Set B is: {8, 2, 4, 6}
Union of A and B is: {1, 2, 3, 4, 5, 6, 8}

We can perform a union of more than two sets at the same time. In such a situation, the set created from union operation contains the elements from each of the sets that are participating in the union operation. For example, if we have set A= {1,2,3,4,5}, set B= {2,4,6,8,10} and set C={ 7,8,9,10} then the resultant set D= A B C will contain elements {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.

We can perform union of more than two sets in python by providing other sets as input to the union() method when it is invoked on a single set as follows.

A = {1, 2, 3, 4, 5}
B = {2, 4, 6, 8}
C = {7, 8, 9, 10}
print("Set A is:", A)
print("Set B is:", B)
print("Set C is:", C)
D = A.union(B, C)
print("Union of A, B and C is:", D)

Output:

Set A is: {1, 2, 3, 4, 5}
Set B is: {8, 2, 4, 6}
Set C is: {8, 9, 10, 7}
Union of A, B and C is: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Intersection Operation

If we are given two sets A and B, the intersection of both sets is calculated as a set that contains those elements that are present in both the set A and set B. We denote a set intersection operation using a   sign.

If we have a set C that is intersection of A and B, we can write C= A B.  For example, suppose that we have set A={1, 2, 3, 4, 5, 6 } and set B = {2, 4, 6,8},  then the set C= A B will contain elements {2, 4, 6 }. You can observe that each element in C belongs to both A or and B. 

We can implement the set intersection operation in python using the intersection() method. The intersection() method when invoked on a set A takes another set B as input argument and returns the set formed by intersection of A and B. This can be observed as follows.

A = {1, 2, 3, 4, 5, 6}
B = {2, 4, 6, 8}
print("Set A is:", A)
print("Set B is:", B)
C = A.intersection(B)
print("Intersection of A and B is:", C)

Output:

Set A is: {1, 2, 3, 4, 5, 6}
Set B is: {8, 2, 4, 6}
Intersection of A and B is: {2, 4, 6}

We can perform an intersection of more than two sets at the same time. In such a situation, the set created from intersection operation contains the elements that are present in every set participating in the intersection operation. For example, if we have set A= {1,2,3,4,5,6}, set B= {2,4,6,8,10} and set C={ 2,4,7,8,9,10} then the resultant set D= A B C will contain elements {2, 4}.

We can perform intersection of more than two sets in python by providing other sets as input to the intersection() method when it is invoked on a single set as follows.

A = {1, 2, 3, 4, 5, 6}
B = {2, 4, 6, 8, 10}
C = {2, 4, 7, 8, 9, 10}
print("Set A is:", A)
print("Set B is:", B)
print("Set C is:", C)
D = A.intersection(B, C)
print("Intersection of A, B and C is:", D)

Output:

Set A is: {1, 2, 3, 4, 5, 6}
Set B is: {2, 4, 6, 8, 10}
Set C is: {2, 4, 7, 8, 9, 10}
Intersection of A, B and C is: {2, 4}

Set Difference Operation

If we are given two sets A and B, the difference of set A and set B is calculated as a set that contains those elements that are present A but are not present in the set B. We denote a set difference operation using a   sign.

If we have a set C that is the difference of A and B, we can write C= A B.  For example, suppose that we have set A={1, 2, 3, 4, 5, 6 } and set B = {2, 4, 6,8},  then the set C= A B will contain elements {1, 3, 5 }. You can observe that each element in C belongs to both A but does not belong to B. 

We can implement the set difference operation in python using the difference() method. The difference() method when invoked on a set A takes another set B as input argument and returns the set formed by difference of A and B. This can be observed as follows.

A = {1, 2, 3, 4, 5, 6}
B = {2, 4, 6, 8}
print("Set A is:", A)
print("Set B is:", B)
C = A.difference(B)
print("Difference of A and B is:", C)

Output:

Set A is: {1, 2, 3, 4, 5, 6}
Set B is: {8, 2, 4, 6}
Difference of A and B is: {1, 3, 5}

Conclusion

In this article, we have discussed different set operations. We have also implemented them in python using sets. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.



Source link

Leave a reply

Please enter your comment!
Please enter your name here