0
0
Pythonprogramming~10 mins

Union and intersection in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a union of two sets.

Python
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 [1] set2
print(result)
Drag options to blanks, or click blank then click option'
A+
B&
C-
D|
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using & instead of | results in intersection, not union.
Using + causes an error because sets don't support +.
2fill in blank
medium

Complete the code to find the intersection of two sets.

Python
a = {10, 20, 30}
b = {20, 40, 50}
common = a [1] b
print(common)
Drag options to blanks, or click blank then click option'
A&
B|
C-
D^
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using | results in union, not intersection.
Using - gives difference, not intersection.
3fill in blank
hard

Fix the error in the code to get the union of two sets.

Python
set_a = {1, 2}
set_b = {2, 3}
union_set = set_a.[1](set_b)
print(union_set)
Drag options to blanks, or click blank then click option'
Aunion
Bintersect
Cadd
Dappend
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'intersect' which is not a valid set method.
Using 'add' or 'append' which are for adding single elements.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 letters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word instead of length for values.
Using <= instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words with their lengths for words longer than 3 letters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = [1]: [2] for w in words if len(w) [3] 3}
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using w.lower() instead of uppercase.
Using <= instead of > in the condition.