Set Operations
Predict Output Medium
Question
What does this code print?
Code
a = {1, 2, 3}
b = {2, 3, 4}
print(len(a & b))
Hint
Intersection of {1,2,3} and {2,3,4} is {2,3}.
Explanation
a & b is the intersection: {2, 3}. len() is 2. Other set operations: | (union), - (difference), ^ (symmetric difference). Sets are optimized for these operations.
Related Resources
Related Lessons
- Data Types
Learn Python data types.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.