0
0
Pythonprogramming~20 mins

Union and intersection in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Master of Union and Intersection
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this union operation?
Consider two sets A and B. What will be printed after the union operation?
Python
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)
A{1, 2, 3}
B{3}
C{1, 2, 4, 5}
D{1, 2, 3, 4, 5}
Attempts:
2 left
๐Ÿ’ก Hint
Union combines all unique elements from both sets.
โ“ Predict Output
intermediate
2:00remaining
What is the output of this intersection operation?
Given two sets X and Y, what will be printed after the intersection operation?
Python
X = {10, 20, 30, 40}
Y = {30, 40, 50, 60}
print(X & Y)
A{30, 40}
B{10, 20}
C{50, 60}
D{10, 20, 30, 40, 50, 60}
Attempts:
2 left
๐Ÿ’ก Hint
Intersection returns only elements common to both sets.
๐Ÿง  Conceptual
advanced
2:00remaining
Which option shows the correct way to find the union of two sets using a method?
You want to find the union of sets set1 and set2 using a set method. Which option is correct?
Aset1.intersection(set2)
Bset1.add(set2)
Cset1.union(set2)
Dset1.update(set2)
Attempts:
2 left
๐Ÿ’ก Hint
Union method returns a new set with all unique elements from both sets.
โ“ Predict Output
advanced
2:00remaining
What is the output of this combined union and intersection code?
Given sets a and b, what will be printed?
Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
result = (a | b) & a
print(result)
A{3, 4}
B{1, 2, 3, 4}
C{5, 6}
D{1, 2}
Attempts:
2 left
๐Ÿ’ก Hint
First union all elements, then intersect with set a.
๐Ÿ”ง Debug
expert
2:00remaining
What error does this code raise?
Examine the code below. What error will it raise when run?
Python
set_a = {1, 2, 3}
set_b = [3, 4, 5]
result = set_a | set_b
print(result)
ATypeError
BSyntaxError
CNameError
DNo error, prints {1, 2, 3, 4, 5}
Attempts:
2 left
๐Ÿ’ก Hint
The union operator requires both operands to be sets.