Home βΊ Problems βΊ Set union and intersection
Set union and intersection
π§ Predict the output Topic: sets Difficulty: easy Free
What this problem practises
Sets are unordered containers that drop duplicates on insertion. Membership tests are fast, insertion order is not preserved, and {} builds an empty dict β not an empty set.
This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.
Difficulty: easy β the fix or the trick is normally a single line, and a careful read should be enough.
The challenge
What will this program print? Type the exact output (both lines).
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The snippet
The interactive editor pre-fills exactly this code β read it, then submit your prediction:
a = {6, 4, 7, 2}
b = {7, 2, 8, 1}
print(len(a | b))
print(sorted(a & b))More sets problems
Keep going
- Output-prediction strategies β how to predict tricky snippets without running them.
- Browse every sets problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.