0
0
Pythonprogramming~10 mins

Difference and symmetric difference 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 find the difference between set_a and set_b.

Python
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
difference = set_a.[1](set_b)
print(difference)
Drag options to blanks, or click blank then click option'
Aunion
Bsymmetric_difference
Cintersection
Ddifference
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'union' instead of 'difference' returns all elements from both sets.
Using 'intersection' returns only common elements.
2fill in blank
medium

Complete the code to find the symmetric difference between set_x and set_y.

Python
set_x = {10, 20, 30}
set_y = {20, 40, 50}
sym_diff = set_x.[1](set_y)
print(sym_diff)
Drag options to blanks, or click blank then click option'
Adifference
Bsymmetric_difference
Cintersection
Dunion
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'difference' returns elements only from the first set.
Using 'union' returns all elements from both sets.
3fill in blank
hard

Fix the error in the code to correctly find the difference of set_m and set_n.

Python
set_m = {7, 8, 9}
set_n = {8, 10}
diff = set_m.[1](set_n)
print(diff)
Drag options to blanks, or click blank then click option'
Adiffrence
Bsymmetric_difference
Cdifference
Dintersect
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Misspelling 'difference' causes AttributeError.
Using 'intersect' instead of 'intersection' causes error.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only 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)
B<=
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' includes shorter words.
Using 'word' as value stores the word, not its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words with length greater than 4.

Python
words = ['tree', 'house', 'car', 'elephant']
result = { [1]: [2] for word in words if len(word) [3] 4 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'word' as key keeps original case.
Using '<' or '<=' includes shorter words.