Lesson 10 of 10
Tuples and sets
- 12 min
- 5 checks
- 3 hints
- Go to task
Goal
Unpacking and unique values
Explanation
A tuple is a list that can't change, like point = (4, 9). You use it for values that belong together and aren't meant to change, such as a point's coordinates or two values returned by a function. Its most useful feature is unpacking: x, y = point hands the items out to the variables in order.
With unpacking you can swap two values in one line: a, b = b, a. Python evaluates the whole right-hand side first and then hands it out, so you don't need a third variable.
A set, on the other hand, keeps each value only once, so set(names) removes duplicate names. Its operators answer common questions directly: a & b gives you the items in both, a | b gives you all the items, and a - b gives you what's only in the first. But a set is unordered, so don't rely on the order it prints in, and sort it with sorted() when you want to display it. Also note that {} is an empty dictionary, not a set; an empty set is written set().
Your task
The two lists were recorded the wrong way around, so swap morning and evening in one line. Then work out these sets: unique_morning for the morning attendees without duplicates, both for those who came to both lectures, everyone for everyone who came at least once, and morning_only for those who came only in the morning.
Checks
5 checks- morning and evening swapped values
- unique_morning holds the morning attendees without duplicates
- both holds those who came to both lectures
- everyone holds everyone who came at least once
- morning_only holds those who came only in the morning