All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Sets: membership, union, intersection and difference
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
A set is an unordered collection with no duplicates. It is ideal for testing membership and for comparing two groups.
Creating sets (spec 8.2.7)
Write a set with braces, or convert another structure with set(). Note that {} on its own is an empty dictionary: use set() for an empty set. Duplicates are removed automatically, and a set has no index, so s[0] is a TypeError.
a = {3, 1, 3, 2}
print(len(a), 2 in a, 5 in a)
b = set((4, 5, 4))
b.add(6)
b.discard(9)
print(len(b))
print(type({}), type(set()))Output:
3 True False
3
<class 'dict'> <class 'set'>remove(x) raises KeyError if x is missing; discard(x) does not.
Set operations
| Operation | Operator | Method | Contains |
|---|---|---|---|
| Union | a | b | a.union(b) | items in either set |
| Intersection | a & b | a.intersection(b) | items in both sets |
| Difference | a - b | a.difference(b) | items in a but not in b |
| Symmetric difference | a ^ b | a.symmetric_difference(b) | items in exactly one of the sets |
Using them
Sets print in an unpredictable order, so sort them for display with sorted().
maths = {"Ana", "Ben", "Cai", "Dev"}
physics = {"Ben", "Dev", "Eve"}
print(sorted(maths | physics))
print(sorted(maths & physics))
print(sorted(maths - physics))
print(sorted(maths ^ physics))Output:
['Ana', 'Ben', 'Cai', 'Dev', 'Eve']
['Ben', 'Dev']
['Ana', 'Cai']
['Ana', 'Cai', 'Eve']Worked example
A = {1, 2, 3, 4}, B = {3, 4, 5}. Find A | B, A & B, A - B and B - A.
- A | B = [1, 2, 3, 4, 5]
- A & B = [3, 4]
- A - B = [1, 2]
- B - A = [5]
Answer: Difference is not symmetric: A - B and B - A are different
Check yourself
- What is displayed when this code runs?
```python
print(len({2, 4, 2, 6, 4}))
```
Show answer
3. Duplicates are removed, leaving {2, 4, 6}: 3 items.
- What is displayed when this code runs?
```python
a = {1, 3, 5, 7}
b = {3, 4, 5, 6}
print(sorted(a & b))
```
Show answer
[3, 5]. The intersection contains items in both sets: [3, 5].
- What is displayed when this code runs?
```python
a = {1, 3, 5, 7}
b = {3, 4, 5, 6}
print(sorted(b - a))
```
Show answer
[4, 6]. b - a keeps the items of b that are not in a: [4, 6].
- What is displayed when this code runs?
```python
a = {"x", "y"}
b = {"y", "z", "w"}
print(len(a | b))
```
Show answer
4. The union holds every distinct item: w, x, y, z: 4.
- What is displayed when this code runs?
```python
vowels = set("aeiou")
print("e" in vowels, "y" in vowels)
```
- True True
- False True
- False False
- True False
Show answer
True False. in tests membership: True False.
- Which exception is raised when this code runs?
```python
s = {1, 2, 3}
s.remove(4)
```
- IndexError
- KeyError
- ValueError
- TypeError
Show answer
KeyError. remove() on a missing item raises KeyError; discard() would not.
- Which line creates an empty set?
- s = {}
- s = []
- s = ()
- s = set()
Show answer
s = set(). {} is an empty dictionary, [] a list and () a tuple.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.