Python – Add Set Items

1. What Does โ€œAdd Set Itemsโ€ Mean?

Adding set items means putting new values into a set.

Remember:
๐Ÿ‘‰ Sets store only unique values.
๐Ÿ‘‰ Duplicate values are not added.

2. Add One Item to a Set (add())

Use add() to add one item.

Example

fruits = {"apple", "banana"}

fruits.add(“orange”)
print(fruits)

3. Add Multiple Items to a Set (update())

Use update() to add many items.

Example

fruits.update(["mango", "grape"])
print(fruits)

4. Add Items from Another Set

Example

set1 = {1, 2}
set2 = {3, 4}
set1.update(set2)
print(set1)

5. Duplicate Items Are Ignored

Example

numbers = {1, 2, 3}
numbers.add(2)
print(numbers)

Output:

{1, 2, 3}

6. Add Items Using Loop

Example

colors = set()

for color in [“red”, “blue”, “green”]:
colors.add(color)

print(colors)

7. Create Empty Set and Add Items

Example

my_set = set()
my_set.add("Python")
my_set.add("Java")
print(my_set)

8. Common Beginner Mistakes

โŒ Using append() with Set

my_set.append("item")

โŒ Error: set has no append.

โŒ Creating Empty Set Wrong Way

s = {}

This creates a dictionary.

โœ” Correct:

s = set()

9. Simple Practice Examples

Example 1: Add City

cities = {"Delhi", "Mumbai"}
cities.add("Kolkata")
print(cities)

Example 2: Add Multiple Numbers

nums = {1, 2}
nums.update([3, 4, 5])
print(nums)

Example 3: Ignore Duplicate

data = {10, 20}
data.add(10)
print(data)

10. Summary (Add Set Items)

โœ” Use add() for one item
โœ” Use update() for many items
โœ” Duplicates are ignored
โœ” Sets store unique values
โœ” Use set() for empty set

๐Ÿ“˜ Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Remove Set Items

  • Join Sets

  • Set Methods (full)

  • Set Exercises

Just tell me ๐Ÿ˜Š

You may also like...