Tuples & Sets
Lesson 4 of 6 · View course roadmap
Learn the idea
Tuples are immutable lists — once created, they cannot change: point = (3, 5). Use them for fixed groups of values. Tuple unpacking is beautiful Python: x, y = point, and swapping is just a, b = b, a.
Sets hold unique values with no order: tags = {"python", "web"}. Adding a duplicate does nothing. Sets are perfect for:
- De-duplicating:
set([1, 2, 2, 3])→{1, 2, 3} - Lightning-fast membership tests:
x in my_set - Set math:
a & b(intersection),a | b(union),a - b(difference)
Where you'll use this
Functions that 'return two values' actually return one tuple — divmod(17, 5) gives (3, 2). Sets power tag systems, deduplicating email lists, and 'which users are in group A but not B' queries.
Common mistakes
- A one-item tuple needs a comma:
(5)is just the number 5;(5,)is a tuple. {}creates an empty dict, not a set — empty set isset().- Sets are unordered: never rely on the order you see when printing one.
Pro tip
Tuple unpacking works in loop headers: for name, score in pairs: — and the swap idiom a, b = b, a needs no temp variable. Interviewers notice when you use it.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +25 XP
The starter has a list with duplicates. Print the number of unique values in it, then print whether 7 is one of them (True/False).
4 True
unique = set(nums); print(len(unique)); print(7 in unique).
nums = [1, 4, 7, 4, 1, 9, 7, 7] unique = set(nums) print(len(unique)) print(7 in unique)
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.