Challenge - 5 Problems
Set Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of set creation with duplicates
What is the output of this Python code?
my_set = {1, 2, 2, 3, 4, 4, 4}
print(my_set)Python
my_set = {1, 2, 2, 3, 4, 4, 4}
print(my_set)Attempts:
2 left
๐ก Hint
Remember that sets do not keep duplicates.
โ Incorrect
Sets automatically remove duplicate values, so only unique elements remain.
โ Predict Output
intermediate2:00remaining
Creating a set from a list
What will be printed by this code?
my_list = [5, 6, 7, 5, 6] my_set = set(my_list) print(my_set)
Python
my_list = [5, 6, 7, 5, 6] my_set = set(my_list) print(my_set)
Attempts:
2 left
๐ก Hint
Converting a list to a set removes duplicates.
โ Incorrect
The set constructor removes duplicates and keeps unique elements only.
โ Predict Output
advanced2:00remaining
Set comprehension with condition
What is the output of this code?
result = {x * x for x in range(5) if x % 2 == 0}
print(result)Python
result = {x * x for x in range(5) if x % 2 == 0}
print(result)Attempts:
2 left
๐ก Hint
Only even numbers from 0 to 4 are squared.
โ Incorrect
The comprehension squares only even numbers: 0, 2, 4 โ 0, 4, 16.
โ Predict Output
advanced2:00remaining
Set creation with mutable elements
What error does this code raise?
my_set = {1, 2, [3, 4]}Python
my_set = {1, 2, [3, 4]}Attempts:
2 left
๐ก Hint
Sets cannot contain mutable elements like lists.
โ Incorrect
Lists are mutable and cannot be added to sets, causing a TypeError.
๐ง Conceptual
expert2:00remaining
Number of items in a set after complex creation
How many items are in the set after running this code?
data = [1, 2, 2, 3, 4, 4, 5]
result = {x for x in data if x % 2 != 0}
print(len(result))Python
data = [1, 2, 2, 3, 4, 4, 5] result = {x for x in data if x % 2 != 0} print(len(result))
Attempts:
2 left
๐ก Hint
Count unique odd numbers in the list.
โ Incorrect
Odd numbers are 1, 3, 5. They are unique, so length is 3.