Lesson 10 of 10

Tuples and sets

Goal

Unpacking and unique values

01

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().

02

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
  1. morning and evening swapped values
  2. unique_morning holds the morning attendees without duplicates
  3. both holds those who came to both lectures
  4. everyone holds everyone who came at least once
  5. morning_only holds those who came only in the morning
03

Hints

0/3
Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.

Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.