```

Python set Data Type

A complete beginner-friendly guide explaining Python sets, their methods, behavior, and real-world use cases.

1. What is a Set?

A set in Python is an unordered collection of unique elements. This means:

# Creating a set
```

numbers = {1, 2, 3, 4}
print(numbers)
```

2. Why Use Sets?

# Removing duplicates using set
```

nums = [1, 2, 2, 3, 3, 4]
unique_nums = set(nums)
print(unique_nums)
```

3. Creating Sets

# Empty set (important!)
```

empty_set = set()

# Set with values

fruits = {"apple", "banana", "mango"}
```

⚠️ {} creates a dictionary, not a set.

4. Common Set Methods

add()

Adds an element to the set

colors = {"red", "blue"}
```

colors.add("green")
```

remove()

Removes an element (error if not found)

colors.remove("red")

discard()

Removes element (no error if missing)

colors.discard("yellow")

pop()

Removes a random element

item = colors.pop()

clear()

Removes all elements

colors.clear()

5. Set Operations

a = {1, 2, 3}
```

b = {3, 4, 5}

print(a | b)   # Union
print(a & b)   # Intersection
print(a - b)   # Difference
print(a ^ b)   # Symmetric Difference
```

6. Real-World Use Cases

When NOT to Use Sets

```