0
0
Pythonprogramming~10 mins

Why sets are used in Python - Test Your Understanding

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

Complete the code to create a set with unique numbers.

Python
numbers = [1]([1, 2, 2, 3, 4, 4])
print(numbers)
Drag options to blanks, or click blank then click option'
Alist
Bset
Cdict
Dtuple
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using list() keeps duplicates.
Using dict() requires key-value pairs.
Using tuple() keeps duplicates and is immutable.
2fill in blank
medium

Complete the code to check if 'apple' is in the set.

Python
fruits = {'apple', 'banana', 'cherry'}
if 'apple' [1] fruits:
    print('Found apple!')
Drag options to blanks, or click blank then click option'
Ain
Bnot in
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '==' compares whole sets, not membership.
Using '!=' is for inequality, not membership.
Using 'not in' checks absence, not presence.
3fill in blank
hard

Fix the error in the code to add an item to the set.

Python
colors = {'red', 'green', 'blue'}
colors.[1]('yellow')
print(colors)
Drag options to blanks, or click blank then click option'
Aappend
Bextend
Cinsert
Dadd
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using append causes an error because sets don't support it.
Using insert or extend are list methods, not set methods.
4fill in blank
hard

Complete the code to create a set of unique words longer than 3 letters.

Python
words = ['apple', 'bat', 'car', 'dog', 'apple']
unique_long = {word for word in words if len(word) [1] 3}
print(unique_long)
Drag options to blanks, or click blank then click option'
B>
C<
Dlen(word)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Putting 'len(word)' in the first blank causes syntax errors.
Using '<' instead of '>' filters shorter words.
5fill in blank
hard

Fill all three blanks to create a set of uppercase words with length greater than 4.

Python
words = ['apple', 'bat', 'carrot', 'dog', 'banana']
result = [1] for word in words if len(word) [2] 4
result_set = set(result)
print(result_set)
Drag options to blanks, or click blank then click option'
Aword.upper()
B>
Cif
Dword.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'word.lower()' instead of 'word.upper()' changes case incorrectly.
Using '<' instead of '>' filters wrong words.
Omitting 'if' causes syntax errors.