B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Add Set Items | Add Single or Multiple Elements

Python Add Set Items

In Python, you can add elements to a set using add() or update(). The add() method adds one item, while update() can add multiple items from iterable objects.

Python में set में items जोड़ने के लिए add() और update() methods का उपयोग होता है। add() एक item जोड़ता है जबकि update() कई items को एक साथ जोड़ता है।

1. Add a Single Element Using add()
fruits = {"apple", "banana"}
fruits.add("mango")
print(fruits)
2. Add Multiple Elements Using update()
fruits.update(["orange", "grape"])
print(fruits)
3. Add from Another Set
set1 = {"apple", "banana"}
set2 = {"kiwi", "melon"}
set1.update(set2)
print(set1)
4. Add Characters from String (Iterable)
chars = {"a", "b"}
chars.update("xyz")
print(chars)
5. Add Tuple Elements to Set
nums = {1, 2}
nums.update((3, 4))
print(nums)