Python 3.3The video for this lesson is on its way.
Everything else is ready: study the parts, the key terms and the quiz below.
- Part 11Five visits, three people
- Part 22set(): each value, once
- Part 33add, remove, discard, and the empty set
- Part 44No order, and no index
- Part 55Union, intersection and difference
- Part 66Fast membership, and the hash
- Part 77What a set can hold: hashable values, and frozenset
- Part 88Recap, your challenge, and next
Python 3.3Code walkthrough8 parts
Sets
Keep unique items and ask membership questions fast.
Loading your progress…Next: 3.4 Dictionaries
In this lesson8 parts
- 01Five visits, three people
- 02set(): each value, once
- 03add, remove, discard, and the empty set
- 04No order, and no index
- 05Union, intersection and difference
- 06Fast membership, and the hash
- 07What a set can hold: hashable values, and frozenset
- 08Recap, your challenge, and next
Key terms
The words this lesson introduces, each in one line. The module’s glossary collects them all.
- difference
- The values in the first set that are not in the second,
a - b. - hash
- A number Python computes from a value, which a set uses to decide where the value is stored. A dictionary uses its keys' hashes in the same way.
- hashable
- A value whose hash never changes, so it can be a set member or a dictionary key: numbers, strings, and tuples of hashable values. Lists, sets and dictionaries are not.
- intersection
- Every value in both sets,
a & b. - set
- An unordered collection in which each value appears at most once. An empty set is
set().
Quiz 5 questions
Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.
Practice
Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.
The self-checking notebook for this lesson is Notebook 2 · Sets and Dictionaries.
Common mistakes
What you will see when it goes wrong, why it happens, and the fix.
Where it’s used
Where this lesson’s ideas turn up in real work.