9. Problem to find symmetric difference of two sets

In this Python set program, we will find the symmetric difference of two sets.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Symmetric difference of two sets: The symmetric difference of set A and B is the set of elements which are in either of the sets A and B, but not in their intersection.

Symmetric difference of two sets: 

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Get the symmetric difference of sets using the  symmetric_difference() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8,9,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Symmetric difference  of a and b: ",a.symmetric_difference(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 1, 7}
Symmetric difference  of a and b:  {2, 4, 5, 7, 8, 9}
				
			

Related Articles

find the difference of two sets.

show if one set is a subset of another set.

Leave a Comment