12. Difference
• Difference in sets is a set where the difference of set x
to y (x - y) is a set of elements that are only in x but not
in y.
• It is performed using the “-” operator and the
difference() method.
13. Example
x = {1,2,3,4,5}
y = {3,4,5,6,7}
print(x-y)
print(x.difference(y))
print(y-x)
print(y.difference(x))
{1, 2}
{1, 2}
{6, 7}
{6, 7}
14. Symmetric Difference
• Symmetric difference in sets is a set where the
symmetric difference of x and y is a set of elements
that is not found in both sets.
• It is performed using the “^” operator and the
symmetric_difference() method.
15. Example
x = {1,2,3,4,5}
y = {3,4,5,6,7}
print(x^y)
print(x.symmetric_difference(y))
print(y^x)
print(y.symmetric_difference(x))
{1, 2, 6, 7}
{1, 2, 6, 7}
{1, 2, 6, 7}
{1, 2, 6, 7}
17. Find the Union, Intersection, Difference and Symmetric
Difference of the following sets:
A = {2,4,12,42,9,17}
B = {3,12,17,5,34,7,10}
C = {34,25,7,42,4,6,11,55}
Exercise
24. Create a program to convert the following lists to a
dictionary. Hint: use zip().
employee = ["Cynthia","Austin","Cesar"]
salary = [40000,30000,45000]
Output:
{'Cynthia': 40000, 'Austin': 30000, 'Cesar': 45000}
Exercise