3) f. Sets Examples




Erica, Enrico’s girlfriend, likes tuples. But she is stubborn and she wants to use something new that Enrico does know about and cannot handle. Erica sets out for… sets: write a set in Python that contains Erica’s favorite colors: red, maroon, wine, crimson.

1
2
Output:
set(['red', 'maroon', 'wine', 'crimson'])




Erica likes red a lot. She likes red that much that she adds it three times (!) in her set. Add the element ‘red’ three times to the set. Then print it and see how the new set looks.

1
2
Output:
set(['red', 'maroon', 'wine', 'crimson'])




Erica thinks that she is repeating herself and wants to remove red from the list! Do that for her! Which method is appropriate? How many different ones? How many times can you remove ‘red’ from the set?

1
2
3
Output:
set(['maroon', 'wine', 'crimson'])
set(['maroon', 'wine', 'crimson'])




Erica makes a different list with Enrico’s favorite colors: red, crimson, orange. Add them all in one, so that Erica can know what kind of bed sheets she can buy next time.

1
2
3
Output:
Buy bedsheets with these colors only =
set(['red', 'maroon', 'wine', 'crimson', 'orange'])




Erica does not like all of the colors that Enrico likes. How can she form a set where only the common favorite colors are included? That is the intersection of the two lists.

1
2
3
Output:
Buy bedsheets with these colors only =
set(['red', 'crimson'])




Enrico is afraid that Erica will buy bed sheets that Enrico will not approve of. For that, he wants to create a ‘blacklist’, i.e. a set with elements all of the elements of Erica’s set that do not appear in Enrico’s set (so that he can exclude them from all future shopping!). How can he do that?

1
2
3
Output:
Enrico: Colors which I must avoid =
set(['orange'])




Erica wants to reconcile with Enrico: she proposes that she will remove ‘wine’ from her favorite colors. Help Enrico perform this in Python; remove one element from the union set.

withcode id=”AVs”]

1
2
3
Output:
Buy bedsheets with these colors =
set(['red', 'maroon', 'crimson', 'orange'])