Lesson 3 of 21 · 48 min · UG / professional
Sets — unique, then compare
Enrolled minus seated is a difference of two unique collections. Duplicates in a roll list are a bug. Union and intersection are the same idea with different keepers.
Sports register
min, max and mean from a table.
You will be able to
- ✓ Treat uniqueness as a policy, not as luck
- ✓ Build a unique list with a membership loop (a set on a laptop)
- ✓ Compute enrolled-minus-seated as a difference of two unique collections
Duplicates in a roll list are a bug
The exam cell asks: who is enrolled but not registered to sit? That is two unique collections. A list of rolls with 12 appearing twice will double-count a hall ticket. On a laptop a set forgets duplicates automatically. Here we simulate that policy: only append if the value is not already there.
Uniqueness is a decision you make at the door. If the source file can contain the same roll twice, you clean it before you print “headcount”. Do not wait for the principal to notice 401 names in a 400-seat hall.
Union, intersection, difference are keeper policies
Difference: in enrolled, not in seated — the missing hall tickets. Intersection: in both — actually sitting. Union: anyone who appears in either list — the combined unique roll for a combined duty list. Full Python spells these as set operators. You should still be able to write the nested membership checks on a whiteboard.
Sets on a laptop are unordered. Do not use a set when invoice order matters. Use it when the question is “is this roll known?”
Words that matter
- Set
- An unordered collection of unique values — the professional type for membership.
- Difference
- In A but not in B — enrolled minus seated.
- Intersection
- In both collections.
- Union
- In either collection, still unique.
Common mistakes
Avoid: Using len(rolls) as headcount when rolls can repeat.
Do this: Unique first, then count.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — skip a duplicate
12 appears twice in the source. seen should hold it once. Unique count is 3.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — enrolled but not seated
19 is enrolled, not in the seated list. Print that roll.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Clean, then count
Headcount is len of the unique list, not of the raw file.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Drop the second 5
Values 5, 8, 5. Build a unique collection. Print Unique 2 as well.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.