0
0
Pythonprogramming~10 mins

Adding and removing set elements 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 add the element 5 to the set.

Python
numbers = {1, 2, 3, 4}
numbers.[1](5)
print(numbers)
Drag options to blanks, or click blank then click option'
Aadd
Bappend
Cinsert
Dextend
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using list methods like append or insert on a set.
2fill in blank
medium

Complete the code to remove the element 3 from the set.

Python
numbers = {1, 2, 3, 4}
numbers.[1](3)
print(numbers)
Drag options to blanks, or click blank then click option'
Apop
Bremove
Cdiscard
Ddelete
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using pop() which removes an arbitrary element.
Using discard() which does not raise an error if element missing.
3fill in blank
hard

Fix the error in the code to correctly add the element 10 to the set.

Python
my_set = {7, 8, 9}
my_set.[1](10)
print(my_set)
Drag options to blanks, or click blank then click option'
Aadd
Bappend
Cinsert
Dextend
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using list methods like append or insert on a set.
4fill in blank
hard

Fill both blanks to remove element 2 safely and add element 5 to the set.

Python
numbers = {1, 2, 3, 4}
numbers.[1](2)
numbers.[2](5)
print(numbers)
Drag options to blanks, or click blank then click option'
Adiscard
Bremove
Cadd
Dpop
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using remove which can cause errors if element is missing.
5fill in blank
hard

Fill all three blanks to add 'apple', remove 'banana' safely, and remove an arbitrary element.

Python
fruits = {'banana', 'cherry'}
fruits.[1]('apple')
fruits.[2]('banana')
removed = fruits.[3]()
print(fruits)
print('Removed:', removed)
Drag options to blanks, or click blank then click option'
Aadd
Bdiscard
Cpop
Dremove
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using remove instead of discard for safe removal.
Using pop incorrectly.