Write a Python program to add member(s) in a set


The program demonstrates how to create and manipulate sets in Python using the add and update methods.

  • A set animal is created using a set literal containing two elements, "Lion" and "Cat". The print() function is used to display the contents of the set, which are displayed in curly braces {} and separated by commas.
  • The add() method is then used to add a single element, "Dog", to the set. The print() function is used to display the contents of the set after the addition, which now contains three elements.
  • The update() method is used to add multiple elements, "Ponda" and "Tiger", to the set. The print() function is used to display the contents of the set after the update, which now contains five elements.

In this program, the methods add and update are used to add elements to the set. The update method can be used to add multiple elements to the set, while the add method can be used to add only a single element.

Source Code

animal = {"Lion","Cat"}
print(animal)
animal.add("Dog")
print("Add single element : ",animal)
animal.update(["Ponda", "Tiger"])
print("Add multiple items : ",animal)

Output

{'Cat', 'Lion'}
Add single element :  {'Cat', 'Lion', 'Dog'}
Add multiple items :  {'Cat', 'Ponda', 'Lion', 'Dog', 'Tiger'}