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]