Adding Items to a Set
You can add items to a set using the add()
method for single items or the update()
method for multiple items.
Using add()
Example: Adding a Single Item
# Adding a single item to a set
numbers = {1, 2, 3}
numbers.add(4)
print("Set after add:", numbers)
Output (Order may vary)
Set after add: {1, 2, 3, 4}
Using update()
Example: Adding Multiple Items
# Adding multiple items to a set
names = {"Karthick AG", "Durai"}
names.update(["Vijay", "John"])
print("Set after update:", names)
Output (Order may vary)
Set after update: {'Vijay', 'John', 'Karthick AG', 'Durai'}
Explanation: The add()
method adds a single item to the set. The update()
method can add multiple items, which can be from lists, tuples, or other sets.