29. Problem to find the symmetric difference between two sets

In this Python set program, we will find the symmetric difference between two sets using the “^” operator with the help of the below-given steps.

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 of 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 between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use “^” to find the symmetric difference between sets using the “^” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {4,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Symmetric difference of two sets using the “^” operator: ",a^b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {1, 4}
Symmetric difference of two sets using the “^” operator:  {2, 5}
				
			

Related Articles

find the union of multiple sets using the | operator.

check if a set is a superset of another set.

Leave a Comment