Sets
About this lesson
A set is a collection of different things, with no order and no repeats. Follow two small sets, A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}, on a Venn diagram. Watch elements land in their zones, then see union, intersection, difference and symmetric difference collect their answers, count a union without double counting, and meet subsets, the universe and the complement, each next to the Python that does it.
A set of elements
A set is a collection of different things, called its elements. We write a set in curly braces: A = {1, 2, 3, 4, 5}. Order does not matter, and each element is simply in the set or not.
>>> A = {1, 2, 3, 4, 5}
>>> A
{1, 2, 3, 4, 5}
>>> {5, 4, 3, 2, 1} == A # order does not matter
True
>>> {1, 1, 2} == {1, 2} # nor does repeating
TrueRead the lesson step by step
A set of elements
A set is a collection of different things, called its elements. We write a set in curly braces: A = {1, 2, 3, 4, 5}. Order does not matter, and each element is simply in the set or not.
>>> A = {1, 2, 3, 4, 5} >>> A {1, 2, 3, 4, 5} >>> {5, 4, 3, 2, 1} == A # order does not matter True >>> {1, 1, 2} == {1, 2} # nor does repeating TrueIs it in the set?
The symbol ∈ means "is an element of". 3 ∈ A is true, because 3 sits inside the box of A. 6 ∉ A, because 6 is not in A. Python asks the same question with the keyword in.
>>> A = {1, 2, 3, 4, 5} >>> 3 in A True >>> 6 in A False >>> 6 not in A TrueA second set, B
Now B = {4, 5, 6, 7}. 4 and 5 are already in A, so the box of B is drawn around them too. Nothing is copied: each element appears once. Overlapping boxes like these are a Venn diagram.
>>> B = {4, 5, 6, 7} >>> set([4, 5, 5, 6, 7, 7]) # duplicates vanish {4, 5, 6, 7} >>> len(B) 4Union: A ∪ B
The union A ∪ B holds every element that is in A, in B, or in both. Python writes it A | B. Each element is listed once, so A ∪ B = {1, 2, 3, 4, 5, 6, 7}.
>>> A = {1, 2, 3, 4, 5} >>> B = {4, 5, 6, 7} >>> A | B {1, 2, 3, 4, 5, 6, 7} >>> A.union(B) {1, 2, 3, 4, 5, 6, 7}Intersection: A ∩ B
The intersection A ∩ B keeps only the elements that are in both sets: the overlap of the two boxes. Python writes it A & B. Here A ∩ B = {4, 5}.
>>> A = {1, 2, 3, 4, 5} >>> B = {4, 5, 6, 7} >>> A & B {4, 5} >>> A.intersection(B) {4, 5}Difference: A − B
The difference A − B keeps what is in A but not in B: take the part of B out of the box of A. So A − B = {1, 2, 3}. Order matters here: B − A = {6, 7}.
>>> A = {1, 2, 3, 4, 5} >>> B = {4, 5, 6, 7} >>> A - B {1, 2, 3} >>> B - A {6, 7}Symmetric difference
The symmetric difference A △ B keeps what is in exactly one of the sets: in A or in B, but not in both. Python writes it A ^ B. It is the union minus the overlap: {1, 2, 3, 6, 7}.
>>> A = {1, 2, 3, 4, 5} >>> B = {4, 5, 6, 7} >>> A ^ B {1, 2, 3, 6, 7} >>> (A | B) - (A & B) {1, 2, 3, 6, 7}Counting with the overlap
The size of a set, written |A|, is how many elements it has: |A| = 5 and |B| = 4. Adding them counts 4 and 5 twice, so take the overlap away once: |A ∪ B| = 5 + 4 − 2 = 7.
>>> A = {1, 2, 3, 4, 5} >>> B = {4, 5, 6, 7} >>> len(A) + len(B) - len(A & B) 7 >>> len(A | B) 7Subsets: C ⊆ A
C is a subset of A, written C ⊆ A, when every element of C is also in A. Take C = {4, 5}: both are in A, so C ⊆ A is true. In Python, C <= A gives True.
>>> A = {1, 2, 3, 4, 5} >>> C = {4, 5} >>> C <= A True >>> C.issubset(A) True >>> {4, 6} <= A # 6 is not in A FalseThe universe and complement
The universe U holds every element we are talking about; here U = {1, 2, ..., 8}. The complement of A is everything in U that is not in A: U − A = {6, 7, 8}.
>>> A = {1, 2, 3, 4, 5} >>> U = set(range(1, 9)) >>> sorted(U - A) # sets print in no set order [6, 7, 8] >>> len(U - A) == len(U) - len(A) True
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01How many elements does the set {3, 1, 3, 2, 1} have?
A set keeps each element once and ignores order, so {3, 1, 3, 2, 1} is the same set as {1, 2, 3}: three elements.
02With P = {1, 2, 3} and Q = {2, 3, 4}, what is P ∩ Q?
The intersection keeps only the elements in both sets: 2 and 3. {1, 2, 3, 4} is the union and {1, 4} the symmetric difference.
03With the same P = {1, 2, 3} and Q = {2, 3, 4}, what is Q − P?
Q − P keeps what is in Q but not in P. Only 4 is in Q and missing from P. Order matters: P − Q would be {1}.
04A class has 12 students who play football and 9 who play chess; 4 play both. How many play at least one?
|F ∪ C| = |F| + |C| − |F ∩ C| = 12 + 9 − 4 = 17. Adding 12 and 9 counts the 4 who play both twice.
05In Python, what does {1, 2} <= {1, 2, 3} give?
For sets, <= asks 'is a subset of'. Both 1 and 2 are in {1, 2, 3}, so the answer is True. It does not compare sizes or numbers.
Glossary
- Set
- A collection of different things with no order. {1, 2} and {2, 1} are the same set, and repeats count once.
- Element
- One thing in a set. 3 ∈ A means 3 is an element of A; 6 ∉ A means it is not.
- Venn diagram
- A drawing of sets as overlapping regions, so elements in both sets sit in the overlap.
- Union (A ∪ B)
- Every element that is in A, in B, or in both. Python: A | B.
- Intersection (A ∩ B)
- Only the elements that are in both A and B. Python: A & B.
- Difference (A − B)
- The elements of A that are not in B. Python: A - B.
- Subset (C ⊆ A)
- C is a subset of A when every element of C is also in A. Python: C <= A.
- Complement
- Everything in the universe U that is not in a set: the complement of A is U − A.
Notes and sources
- Building a set throws duplicates away: set([1, 2, 2, 3]) gives {1, 2, 3}. A set has no order either, so {1, 2} == {2, 1} is True.
- The empty set, written ∅, has no elements. In Python it is set(); {} on its own makes an empty dict, not a set.
- Every set is a subset of itself, and ∅ is a subset of every set. A ∩ B is always a subset of both A and B.
- Symbols and Python: ∈ is in, ∪ is |, ∩ is &, − is -, △ is ^ and ⊆ is <=.
- A Python set can hold numbers, strings and tuples, but not lists, because its elements must be hashable (unchangeable).
- Lehman, Leighton & Meyer, Mathematics for Computer Science (MIT 6.042J), section 4.1 Sets
- MIT OpenCourseWare 6.042J Mathematics for Computer Science (Spring 2015)
- Hammack, Book of Proof, 3rd ed., chapter 1: Sets
- Python library: Set types (set, frozenset) and their operators
- Python tutorial: Data structures, Sets
Quiz Unlocks after the last step.