Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using list() keeps duplicates.
Using dict() requires key-value pairs.
Using tuple() keeps duplicates and is immutable.
โ Incorrect
Using set() removes duplicates and creates a collection of unique items.
2fill in blank
mediumComplete 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'
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.
โ Incorrect
The in keyword checks if an item exists in a set.
3fill in blank
hardFix 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'
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.
โ Incorrect
Sets use add() to add a single item.
4fill in blank
hardComplete 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Putting 'len(word)' in the first blank causes syntax errors.
Using '<' instead of '>' filters shorter words.
โ Incorrect
The set comprehension uses word directly and filters words with length greater than 3.
5fill in blank
hardFill 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'
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.
โ Incorrect
The comprehension converts words to uppercase, filters by length greater than 4, and uses 'if' for the condition.