Prerequisite:
Python set() Function with Examples
Set symmetric_difference() Method in Python:
The symmetric_difference() method returns a set that includes all items from both sets but excludes items that are present in both sets.
That means the returned set contains a mix of items that aren’t in either set.
For Example:
If P and Q are two distinct sets. The Symmetric difference of given two sets is :
The set of elements that are in either P or Q but not in their intersection is the symmetric difference.
Let P={4, 5, 6, 7}
Q={5, 6, 8, 9}
Here {5, 6} are the common elements in both sets. so, we exclude them.
The symmetric difference of P and Q = {4, 7, 8, 9}
Syntax:
set.symmetric_difference(set)
Parameters
set: This is Required. The set to look for matches in.
Examples:
Example1:
Input:
Given first set = {10, 20, 50, 60, 30}
Given second set = {20, 60, 80}Output:
The symmetric difference of first and secondset = {10, 80, 50, 30}Example2:
Input:
Given first set = {4, 5, 6, 7}
Given second set = {5, 6, 8, 9}Output:
The symmetric difference of first and secondset = {4, 7, 8, 9}Set symmetric_difference() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first set as static input and store it in a variable.
- Give the second set as static input and store it in another variable.
- Give the third set as static input and store it in another variable.
- Get the symmetric difference of the first set and second set using the symmetric_difference() method and print it.
- Get the symmetric difference of the second set and first set using the symmetric_difference() method and print it.
- Get the symmetric difference of the first set and third set using the symmetric_difference() method and print it.
- The Exit of the Program.
Below is the implementation:
# Give the first set as static input and store it in a variable.
fst_set = {10, 20, 50, 60, 30}
# Give the second set as static input and store it in another variable.
scnd_set = {20, 60, 80}
# Give the third set as static input and store it in another variable.
thrd_set = {}
# Get the symmetric difference of the first set and second set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and secondset = ",
fst_set.symmetric_difference(scnd_set))
# Get the symmetric difference of the second set and first set using
# the symmetric_difference() method and print it.
print("The symmetric difference of second and firstset = ",
scnd_set.symmetric_difference(fst_set))
# Get the symmetric difference of the first set and third set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and thirdset = ",
fst_set.symmetric_difference(thrd_set))
Output:
The symmetric difference of first and secondset = {10, 80, 50, 30}
The symmetric difference of second and firstset = {80, 50, 10, 30}
The symmetric difference of first and thirdset = {10, 50, 20, 60, 30}Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first set as user input using set(),map(),input(),and split() functions.
- Store it in a variable.
- Give the second set as user input using set(),map(),input(),and split() functions.
- Store it in another variable.
- Give the third set as user input using set(),map(),input(),and split() functions.
- Store it in another variable.
- Get the symmetric difference of the first set and second set using the symmetric_difference() method and print it.
- Get the symmetric difference of the second set and first set using the symmetric_difference() method and print it.
- Get the symmetric difference of the first set and third set using the symmetric_difference() method and print it.
- The Exit of the Program.
Below is the implementation:
# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
'Enter some random Set Elements separated by spaces = ').split()))
# Get the symmetric difference of the first set and second set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and secondset = ",
fst_set.symmetric_difference(scnd_set))
# Get the symmetric difference of the second set and first set using
# the symmetric_difference() method and print it.
print("The symmetric difference of second and firstset = ",
scnd_set.symmetric_difference(fst_set))
# Get the symmetric difference of the first set and third set using
# the symmetric_difference() method and print it.
print("The symmetric difference of first and thirdset = ",
fst_set.symmetric_difference(thrd_set))
Output:
Enter some random Set Elements separated by spaces = 3 4 5 6
Enter some random Set Elements separated by spaces = 3 7 8 9
Enter some random Set Elements separated by spaces = 9 5 7 1
The symmetric difference of first and secondset = {4, 5, 6, 7, 8, 9}
The symmetric difference of second and firstset = {4, 5, 6, 7, 8, 9}
The symmetric difference of first and thirdset = {1, 3, 4, 6, 7, 9}