The following exercises cover Sets in Python.
Exercise 1
Complete the syntax to create a set of strings, myset.
myset = 'apple', 'banana', 'cherry'
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.')
Exercise 3
Add an item 'mango' to the set myset using Set add() method.
myset = {'apple', 'banana', 'cherry'} .('mango')
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)
Exercise 5
How to remove the item 'banana' form the set myset?
myset = {'apple', 'banana', 'cherry'} .('banana')
Exercise 6
Iterate over the items of the set myset using For loop.
myset = {'apple', 'banana', 'cherry'} for item : print(item)