Exercise 207

Given a set A = { 'a', 'b', 'c', 'd' }. Write an algorithm in Python that adds an element 'x' to A without using the add() method.

Solution

A = { 'a', 'b', 'c', 'd' }

# we add 'x' to A via the union() method
A2 = A.union({'x'})

# show set A2
print(A2) # output: {'a', 'd', 'b', 'c', 'x'}
Younes Derfoufi
my-courses.net

Leave a Reply