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 delattr()
Python Program to Find Sum of Natural Numbers Using Recursion
Python String casefold()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python print()
Python Program to Convert Bytes to a String
Debug a JavaMail Program
Java Program to Implement the Program Used in grep/egrep/fgrep
Machine Learning with Python for everyone - Mark E.Fenner
Python String isalnum()
Python Program to Return Multiple Values From a Function
Python List reverse()
Python String replace()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Dictionary get()
Python Program to Count the Occurrence of an Item in a List
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Input, Output and Import
Python Set intersection_update()
Python str()
Python Shallow Copy and Deep Copy
Python String ljust()
Python Statement, Indentation and Comments
Python Program to Add Two Matrices
Python callable()
Python Program to Print Hello world!
Python Program to Find Hash of File
Python Program to Find the Size (Resolution) of a Image
Python issubclass()
Python Package
Python Program to Find Factorial of Number Using Recursion