IB Computer Science HL · Year 2 · Lesson 27

Sets as ADTs

Construct and apply Python sets using uniqueness, membership, union, intersection, difference, subset and superset operations.

Paper 250 minutesB4.1.5 · B4.1.6
Targets

Use the ADT's behaviour deliberately

2 / 10
  • 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.
B4.1.5B4.1.6
Learn + retrieve

Rapid Recall Deck

3 / 10

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”.

Operations

Union, intersection and difference answer different questions

4 / 10
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)
Construct

Membership and set relationships in Python

5 / 10

Construct membership + update

Given codes = {"LHR", "SIN", "LAX"}, construct code that:

  1. prints whether "SIN" is present;
  2. adds "AUS";
  3. 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)
Apply

Uniqueness is useful behaviour, not just a definition

6 / 10
ApplicationAn airline has several flight passenger-email lists. The same person may appear on multiple flights, but a promotion should be sent to each email address only once.

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"]
Create an empty set.
Add every email from both lists.
Explain why duplicates disappear.
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.

Trace + reason

Python behaviour still has edge cases

7 / 10

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.

Exam lens

Practice the set behaviour used in the specimen

8 / 10

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.

  1. False.
  2. {2, 6}.
  3. 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.
Exit check

Choose the operation from the meaning

9 / 10

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
Homework

Finish the learning cycle

10 / 10

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.