Sets Debugging#

Section Title: Sets Debugging

  • Each of the following short code contains one or more bugs.     

  • Please identify and correct these bugs.

  • Provide an explanation for your answer.

Question-1#

letters = {'a', 'b', 'c'}

print(letters[1])

Solution

There is no indexing for sets.

Question-2#

letters = {'a', 'b', 'c'}

letters.append('d')

print(letters)

Solution

Sets do not have an append() method; instead, they use the add() method.

Question-3#

letters = {'a', 'b', ['c','d']}

print(letters)

Solution

A list cannot be an element of a set.

Question-4#

numbers = {1,2,3,4,'5'}

print(sum(numbers))

Solution

‘5’ is a string and cannot be added to integers.

Question-5#

letters = {'a', 'b', 'c'}

letters.pop(1)

print(letters)

Solution

The pop() method has no parameters.