Chapters
Python114 chapters

Exercises

Set Methods

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Print who joined and who left, sorted, using set differences.

Python
before = {"ada", "grace"}
after = {"grace", "kat"}
# print the joiners, then the leavers
Exercise 2

Remove an item that may not be there, without raising.

Python
tags = {"a", "b"}
# remove "missing" safely, then print the sorted set
Exercise 3

Use a frozenset so the pair can be a dictionary key, then look it up in the other order.

Python
lookup = {}
# map the pair 1 and 2 to "first", then print the lookup using 2 and 1