Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Union and Intersection of Sets
๐ Scenario: You are organizing a community event and have two lists of people who signed up for two different activities. You want to find out who signed up for either activity and who signed up for both.
๐ฏ Goal: Build a Python program that finds the union and intersection of two sets of names.
๐ What You'll Learn
Create two sets with exact names
Create a variable for union of the two sets
Create a variable for intersection of the two sets
Print the union and intersection sets
๐ก Why This Matters
๐ Real World
Finding union and intersection is useful when combining or comparing groups of people, items, or data in real life, like event planning or inventory management.
๐ผ Career
Understanding set operations is important in data analysis, software development, and database management where you often need to combine or filter data.
Progress0 / 4 steps
1
Create two sets of names
Create a set called activity1 with these exact names: 'Alice', 'Bob', 'Charlie'. Also create a set called activity2 with these exact names: 'Bob', 'Diana', 'Edward'.
Python
Hint
Use curly braces {} to create sets and separate names with commas.
2
Create union and intersection variables
Create a variable called union_set that stores the union of activity1 and activity2. Create another variable called intersection_set that stores the intersection of activity1 and activity2.
Python
Hint
Use the .union() method for union and .intersection() method for intersection.
3
Print the union and intersection sets
Write two print statements: one to display union_set and one to display intersection_set.
Python
Hint
Use print() to show the sets on the screen.
4
Run and check the output
Run the program and check that the output shows the union set with all unique names and the intersection set with names common to both activities.
Python
Hint
The first printed set should have all five unique names. The second printed set should have only 'Bob'.
Practice
(1/5)
1. What does the union operation do when applied to two sets in Python?
easy
A. Combines all unique elements from both sets
B. Finds elements only in the first set
C. Finds elements only in the second set
D. Finds elements common to both sets
Solution
Step 1: Understand union operation
The union of two sets includes every unique element from both sets without duplicates.
Step 2: Compare with other options
Options B and C describe subsets, and D describes intersection, not union.
Final Answer:
Combines all unique elements from both sets -> Option A
Quick Check:
Union = all unique elements combined [OK]
Hint: Union joins all unique items from both sets [OK]
Common Mistakes:
Confusing union with intersection
Thinking union only takes elements from one set
Assuming union keeps duplicates
2. Which of the following is the correct syntax to find the intersection of two sets a and b in Python?
easy
A. a + b
B. a | b
C. a & b
D. a - b
Solution
Step 1: Recall intersection syntax
In Python, the intersection of two sets is found using the & operator or the .intersection() method.
Step 2: Check other operators
| is union, + is invalid for sets, - is difference.
Python sets do not support the + operator; this causes a TypeError.
Step 2: Correct way to union sets
Use | operator or .union() method to combine sets.
Final Answer:
Sets cannot be added with + operator -> Option A
Quick Check:
+ operator invalid for sets [OK]
Hint: Use | or .union(), not + for sets [OK]
Common Mistakes:
Trying to add sets with +
Ignoring error message
Confusing list addition with set union
5. Given two lists of student names, list1 = ['Anna', 'Bob', 'Cara'] and list2 = ['Bob', 'Diana', 'Anna'], which Python code correctly finds students present in both lists using set operations?
hard
A. list1 | list2
B. set(list1) & set(list2)
C. set(list1) + set(list2)
D. list1.intersection(list2)
Solution
Step 1: Convert lists to sets for set operations
Lists must be converted to sets to use intersection (&) operator.
Step 2: Use & operator to find common elements
set(list1) & set(list2) returns elements in both sets.
Step 3: Check other options
list1 | list2 uses | on lists (invalid), C uses + on sets (invalid), D calls intersection on list (no such method).
Final Answer:
set(list1) & set(list2) -> Option B
Quick Check:
Convert lists to sets, then & for intersection [OK]
Hint: Convert lists to sets before intersection [OK]