Sets as ADTs
Construct and apply Python sets using uniqueness, membership, union, intersection, difference, subset and superset operations.
Use the ADT's behaviour deliberately
- explain unordered uniqueness as the defining behaviour of a set;
- construct union, intersection and difference operations;
- construct membership, add, remove, subset and superset checks;
- apply a set to a realistic duplicate-elimination or membership problem.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
Union, intersection and difference answer different questions
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a | b)
print(a & b)
print(a - b)Predict the three mathematical results
Because sets are unordered, write the elements that must be present; do not rely on a particular printed order.
- Union: (1, 2, 3, 4, 5)
- Intersection: (3, 4)
- Difference
a - b: (1, 2)
Membership and set relationships in Python
Construct membership + update
Given codes = {"LHR", "SIN", "LAX"}, construct code that:
- prints whether
"SIN"is present; - adds
"AUS"; - removes
"LAX".
print("SIN" in codes)
codes.add("AUS")
codes.remove("LAX")Construct subset/superset checks
Given required = {"email", "name"} and fields = {"email", "name", "grade"}, construct one Python expression that checks whether all required fields are present and one expression that checks whether fields is a superset of required.
required.issubset(fields) fields.issuperset(required)
Uniqueness is useful behaviour, not just a definition
Apply a set
Explain the algorithm, then construct Python code for the data below.
flight1 = ["a@x.com", "b@x.com"] flight2 = ["b@x.com", "c@x.com"]
unique_emails = set()
for email in flight1:
unique_emails.add(email)
for email in flight2:
unique_emails.add(email)The final set contains a@x.com, b@x.com and c@x.com exactly once because a set cannot contain duplicate elements.
Python behaviour still has edge cases
Trace uniqueness
s = {2, 6, 9}
s.add(6)
s.add(11)
print(6 in s)State the membership output and the mathematical contents of s after the two additions.
The membership test prints True. The set contains (2, 6, 9, 11); adding 6 again does not create a duplicate.
Remove safely
The program may attempt to remove an email that is absent. Explain one difference between remove() and discard() in Python and choose the safer option for this requirement.
remove(x) raises an error if x is absent, whereas discard(x) leaves the set unchanged. If absence is an allowed condition, discard() is safer.
Practice the set behaviour used in the specimen
Specimen-aligned mini-set
Given set1 = {3, 6, 2, 9, 10} and set2 = {2, 6, 1, 11}:
1. State the result of 12 in set1.
2. State the mathematical result of set1 & set2.
3. Explain how a set can ensure that only one promotional email is sent to each address when passengers may appear on several flight lists.
False.{2, 6}.- Add each email address to one set before sending. Repeated addresses collapse to one element because sets enforce uniqueness, so iteration over the set sends at most one message per address.
Choose the operation from the meaning
Exit construct
Given sets registered and submitted, construct one expression that returns the students who registered but did not submit, and one expression that returns students present in both sets.
registered - submitted registered & submitted
Finish the learning cycle
IA — main task
Continue Criterion D development/testing and check whether any collection in your solution should enforce uniqueness rather than preserve duplicates.
Paper 2 — short practice
Redo all required set operations from memory: union, intersection, difference, membership, add, remove, subset and superset.