Collections · Lesson 15 of 25
Tuples and sets
Fixed sequences and collections of unique values.
8 minutes2 games
A tuple is an immutable sequence, useful for coordinates or multiple return values. A set holds unique values without a guaranteed order, useful for deduplication and membership. set() creates an empty set; {} creates an empty dictionary.
Python
point = (3, 7)
x, y = point
print(x, y)
unique = set(["python", "css", "python"])
print(sorted(unique))
print("css" in unique)
print(sorted({1, 2, 3} & {2, 3, 4}))| Tool | Use |
|---|---|
tuple | Ordered; its elements cannot be reassigned |
set | Unique values; no indexing |
| Intersection / union |
What's the output?
print(len(set([1, 1, 2, 3, 3])))
What will this code print?
Exercise
Remove duplicates and print the numbers as a sorted list.
Expected output: [1, 2, 3]
Python
nums = [3, 1, 3, 2]
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.