AQA 7517, section 4.4.2.2
Regular expressions (4.4.2.3) describe sets of strings. To understand them you need the basic mathematics of sets.
What is a set?
A set is an unordered collection of values in which each value occurs at most once.
- Order doesn't matter: .
- Repeats are ignored: is the same set as .
- Several programming languages have sets built in. In Python, curly braces make a set:
A = {1, 2, 3}
print(2 in A) # True
print(A) # {1, 2, 3}
Set comprehension
A set can be described by a rule instead of listing every element. This is set comprehension:
Read this as " is the set of objects such that is a natural number and ." The symbols mean:
| Symbol | Meaning |
|---|---|
| "such that" | |
| "is a member of" | |
| the natural numbers | |
| AND |
So .
Python has the same idea. This builds the set by doubling each member of :
doubled = {2 * x for x in {1, 2, 3}}
print(doubled) # {2, 4, 6}
The empty set
The empty set has no elements. It is written or .
Compact representation of a set
Sets of strings can be written compactly. For example:
This is the set of all strings made of zeros followed by ones, where is at least 1. The little means "repeat times":
It contains every string with an equal number of 0s and 1s, with all the 0s first.
Finite and infinite sets
| Type | Meaning | Example |
|---|---|---|
| Finite set | Its elements can be counted off up to some number: 1st, 2nd, ..., last | |
| Infinite set | Never ends | , the natural numbers |
| Countably infinite set | Infinite, but can be counted off using the natural numbers | , the even numbers |
| Uncountable set | Infinite and cannot be counted off | , the real numbers |
The cardinality of a finite set is the number of elements in it. It is written . If then .
A countable set is one with the same cardinality as some subset of the natural numbers.
Cartesian product
The Cartesian product of two sets and , written (read " cross "), is the set of all ordered pairs where is a member of and is a member of .
If and :
The size of the product is .
X = {1, 2}
Y = {"a", "b"}
product = {(x, y) for x in X for y in Y}
print(len(product)) # 4
Subsets
- means is a subset of : every element of is also in . This includes the case where .
- means is a proper subset of : is a subset of , but contains at least one element not in .
Set operations
| Operation | Symbol | Meaning |
|---|---|---|
| Membership | is an element of | |
| Union | everything in , or in , or in both | |
| Intersection | only the elements in both and | |
| Difference | the elements in that are not in |
The set difference can also be written , and is defined as .
For and :
| Expression | Result |
|---|---|
A = {1, 2, 3, 4}
B = {3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5} union
print(A & B) # {3, 4} intersection
print(A - B) # {1, 2} difference
print(3 in A) # True membership
Worked example
Task: let and . Find , , and . Is a proper subset of ?
- : yes. Every element of is in , and also contains 2 and 6, so it's a proper subset.
Exam tip: learn the symbols well: (member of), (union), (intersection), (difference), (subset) and (proper subset). Write elements inside curly brackets, and remember sets never list the same element twice.
Check yourself
What is a set?
An unordered collection of values in which each value occurs at most once.
What is the cardinality of ?
4: the number of elements.
Find the Cartesian product of and
.
What is the difference between a subset and a proper subset?
A subset can be equal to the whole set. A proper subset is a subset that is not equal to the whole set, so the larger set has at least one extra element.
Give an example of a countably infinite set and an uncountable set
The natural numbers are countably infinite. The real numbers are uncountable.