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

  1. 01Five visits, three people
  2. 02set(): each value, once
  3. 03add, remove, discard, and the empty set
  4. 04No order, and no index
  5. 05Union, intersection and difference
  6. 06Fast membership, and the hash
  7. 07What a set can hold: hashable values, and frozenset
  8. 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.