In this example, we have defined two set variables and we have performed different set operations: union, intersection, difference and symmetric difference.
To understand this example, you should have the knowledge of the following Python programming topics:
Python offers a datatype called set whose elements must be unique. It can be used to perform different set operations like union, intersection, difference and symmetric difference.
Source Code
# Program to perform different set operations like in mathematics # define three sets E = {0, 2, 4, 6, 8}; N = {1, 2, 3, 4, 5}; # set union print("Union of E and N is",E | N) # set intersection print("Intersection of E and N is",E & N) # set difference print("Difference of E and N is",E - N) # set symmetric difference print("Symmetric difference of E and N is",E ^ N)
Output
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8} Intersection of E and N is {2, 4} Difference of E and N is {8, 0, 6} Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}
In this program, we take two different sets and perform different set operations on them. This can equivalently done by using set methods.
Related posts:
Python String find()
Python bytearray()
Python Program to Get the Class Name of an Instance
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python String encode()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python String count()
Python enumerate()
Python Statement, Indentation and Comments
Python memoryview()
Python String istitle()
Python id()
Python String isprintable()
Python Program to Display Fibonacci Sequence Using Recursion
Python getattr()
Python Program to Slice Lists
Python datetime
Python hex()
Python Program to Display Calendar
Node.js vs Python for Backend Development
Python isinstance()
Python list()
Python Program to Differentiate Between del, remove, and pop on a List
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Remove Punctuations From a String
Python Tuple index()
Python String maketrans()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String title()
Python *args and **kwargs
Python Program to Sort a Dictionary by Value