The following exercises cover Sets in Python.
Exercise 1
Complete the syntax to create a set of strings, myset
.
myset = 'apple', 'banana', 'cherry'
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 2
Check if item 'banana'
is present in the set, myset
, using membership operator – in.
myset = {'apple', 'banana', 'cherry'} if 'banana' : print('banana is in the set.')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 3
Add an item 'mango'
to the set myset
using Set add() method.
myset = {'apple', 'banana', 'cherry'} .('mango')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 4
Which method is used to add the items of a set set2
to the set set1
?
set1 = {'apple', 'banana', 'cherry'} set2 = {'mango', 'grapes', 'apple'} set1.(set2)
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 5
How to remove the item 'banana'
form the set myset
?
myset = {'apple', 'banana', 'cherry'} .('banana')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 6
Iterate over the items of the set myset
using For loop.
myset = {'apple', 'banana', 'cherry'} for item : print(item)
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.