Sets
A set is a collection of distinct objects, its elements, with no order and no repetition. Sets are the basic data type of mathematics: numbers, functions, relations and graphs are all defined from them. Python's set type follows the same rules, which makes it easy to experiment.
Notation
| Notation | Meaning |
|---|---|
| is an element of | |
| is not an element of | |
| the set listed by its elements (roster notation) | |
| set-builder notation: the integers whose square is below 10 | |
| the empty set | |
| the number of elements (cardinality) of a finite set |
Standard number sets: (this course includes 0, as computer scientists usually do), the integers, the rationals, the reals, the complex numbers.
Since order and repetition do not matter, : both have exactly the elements 1, 2, 3, and .
A = {1, 2, 2, 3}
print(A, len(A), A == {3, 2, 1})
B = {x for x in range(-5, 6) if x * x < 10} # set-builder in Python
print("{x in Z : x^2 < 10} =", sorted(B))
print("3 in B?", 3 in B, " 4 in B?", 4 in B)
print("empty set:", set(), len(set())){1, 2, 3} 3 True
{x in Z : x^2 < 10} = [-3, -2, -1, 0, 1, 2, 3]
3 in B? True 4 in B? False
empty set: set() 0
Note: In Python
{}is an empty dictionary; the empty set isset().
Subsets and equality
( is a subset of ) means every element of is in : . (a proper subset) means and . Two sets are equal exactly when each is a subset of the other:
This gives the standard way to prove two sets are equal: show "" in both directions. Every set has and itself as subsets ( is vacuously true).
The power set
The power set is the set of all subsets of . If , then : each element is either in a subset or not, two choices per element. This matches the bitmask trick: a subset of an -element list is an -bit number.
items = ["a", "b", "c"]
n = len(items)
subsets = [{items[i] for i in range(n) if mask >> i & 1} for mask in range(2 ** n)]
for mask, s in enumerate(subsets):
print(f"{mask:03b} -> {sorted(s)}")
print("|P(A)| =", len(subsets))000 -> []
001 -> ['a']
010 -> ['b']
011 -> ['a', 'b']
100 -> ['c']
101 -> ['a', 'c']
110 -> ['b', 'c']
111 -> ['a', 'b', 'c']
|P(A)| = 8
Elements of a set can be sets themselves: has two elements, and (the first has 0 elements, the second has 1).
Russell's paradox
Can any description define a set? Consider , the set of all sets that are not members of themselves. Is ? If yes, then by definition ; if no, then satisfies the condition, so . Both answers are contradictory. Modern set theory (ZFC) avoids this by allowing set-builder notation only to select elements from an existing set: .
In software
Sets give average membership tests via hashing, so if user_id in seen: with a set is much faster than with a list. Python sets only hold hashable (immutable) values; frozenset is an immutable set that can itself be an element of a set, which is how you build as a real Python set.
Exercises
Exercise 1: Roster form
List the elements of (a) , (b) , (c) .
Answer
(a) ; (b) ; (c) .
Solution
(a) uses . (b) both and square to 4. (c) is not a natural number, so no element satisfies the condition.
Exercise 2: Count elements
What are , , and ?
Answer
1, 2, 16, 1.
Solution
contains one element, the empty set. contains two different elements. A 4-element set has subsets. The empty set has exactly one subset, itself, so and .
Exercise 3: Power set with frozensets (code)
Build as a Python set of frozensets and check that it has 8 elements and contains both and .
Answer
8, True, True.
Solution
from itertools import combinations
A = {1, 2, 3}
P = {frozenset(c) for k in range(len(A) + 1) for c in combinations(A, k)}
print(len(P), frozenset() in P, frozenset(A) in P)8 True True
Test yourself
What is ?
- 5
- 3
- 2
- 4
How many subsets does a 10-element set have?
- 10
- 100
- 1024
Which statement is true for every set ?