Previous Lesson Current Lesson Next Lesson

Question 1

s = {1, "cat", 2, 1}

Because this is a set, the duplicate 1 at the end is omitted. Thus, there are only 3 (unique) elements in this set.

Question 2

Building on from the previous solution, we have:

s = {1, "cat", 2, 1}

s.add(3)
s.add("house")
s.add("cat")

s.remove(1)

3 and "house" are unique to the set, so they will be added. "cat" is already in the set, so it will not be added again. We then remove 1, so the new length is 3 (the initial set only has 3 unique elements) plus 2 (number of new, unique elements) minus 1 (for the removed element), for a total of 4 elements.