0
0
Pythonprogramming~20 mins

Set creation in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Set Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
A{1, 2, 3, 4}
B{1, 2, 2, 3, 4, 4, 4}
C[1, 2, 3, 4]
D(1, 2, 3, 4)
Attempts:
2 left
๐Ÿ’ก Hint
Remember that sets do not keep duplicates.
โ“ Predict Output
intermediate
2: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)
A(5, 6, 7)
B[5, 6, 7, 5, 6]
C{5, 6, 7}
D{5, 6, 7, 5, 6}
Attempts:
2 left
๐Ÿ’ก Hint
Converting a list to a set removes duplicates.
โ“ Predict Output
advanced
2: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)
A{0, 2, 4, 6, 8}
B{1, 9, 25}
C{0, 1, 4, 9, 16}
D{0, 4, 16}
Attempts:
2 left
๐Ÿ’ก Hint
Only even numbers from 0 to 4 are squared.
โ“ Predict Output
advanced
2: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]}
ATypeError
BSyntaxError
CValueError
DNo error, set created
Attempts:
2 left
๐Ÿ’ก Hint
Sets cannot contain mutable elements like lists.
๐Ÿง  Conceptual
expert
2: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))
A7
B3
C4
D5
Attempts:
2 left
๐Ÿ’ก Hint
Count unique odd numbers in the list.