Here is a clear, complete, and exam-oriented explanation of Sets in Python, perfect for BCA/MCA/B.Tech students.
Sets in Python
A set in Python is an unordered, mutable, and collection of unique elements.
Sets are used when:
- You want to store unique values
- You don’t care about order
- You need fast membership testing
Sets are implemented using hash tables, which makes them very fast for operations like searching.
1. Creating a Set
Sets are created using curly braces { } or the set() function.
Example:
s = {1, 2, 3, 4}
print(s)
Mixed data types:
s = {"apple", 10, 3.14}
Creating an empty set:
s = set() # Correct
❌ Wrong:
s = {} # This creates an empty dictionary, not a set
2. Characteristics of Sets
✔ Unordered → no index, no slicing
✔ Mutable → can add/remove elements
✔ Unique → duplicates automatically removed
✔ Does not support indexing → s[0] ❌ error
Example:
s = {1, 2, 2, 3}
print(s) # Output: {1, 2, 3}
3. Accessing Set Elements
Because sets are unordered, you cannot access elements by index.
You can access elements using a loop:
s = {10, 20, 30}
for x in s:
print(x)
4. Adding Elements
add() → add single item
s = {1, 2}
s.add(3)
update() → add multiple items
s.update([4, 5], {6, 7})
5. Removing Elements
remove(x) → removes item; gives error if not found
s.remove(3)
discard(x) → removes item; no error if item not found
s.discard(10)
pop() → removes a random element
s.pop()
clear() → remove all elements
s.clear()
6. Set Operations (Very Important for Exams)
Python supports mathematical set operations.
1. Union (| or union())
Combines all unique elements from two sets.
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}
print(a.union(b)) # same
2. Intersection (& or intersection())
Common elements.
print(a & b) # {3}
print(a.intersection(b))
3. Difference (- or difference())
Elements in a but not in b.
print(a - b) # {1, 2}
print(a.difference(b))
4. Symmetric Difference (^ or symmetric_difference())
Elements not common to both sets.
print(a ^ b) # {1, 2, 4, 5}
7. Set Membership
s = {1, 2, 3}
print(2 in s) # True
print(5 not in s) # True
8. Frozenset (Immutable Set)
frozenset is an immutable version of a set.
fs = frozenset([1, 2, 3])
- Cannot add/delete elements
- Can be used as dictionary keys
- Supports all set operations
9. Built-in Set Methods
| Method | Description |
|---|---|
| add() | Add element |
| update() | Add multiple elements |
| remove() | Remove item (error if not found) |
| discard() | Remove item (no error) |
| pop() | Remove random item |
| clear() | Remove all items |
| union() | Set union |
| intersection() | Common items |
| difference() | Items in one set only |
| symmetric_difference() | Uncommon items |
| issubset() | Check if a set is subset |
| issuperset() | Check if superset |
| isdisjoint() | True if no common elements |
10. Practical Examples
Example 1: Remove duplicates from a list
lst = [1, 2, 2, 3, 4, 4]
unique = list(set(lst))
print(unique)
Example 2: Testing subset
a = {1, 2}
b = {1, 2, 3, 4}
print(a.issubset(b)) # True
Example 3: Finding common characters
set("python") & set("typing")
Summary Table: Sets
| Property | Detail |
|---|---|
| Ordered | ❌ No |
| Mutable | ✔ Yes |
| Allow duplicates | ❌ No |
| Indexing allowed | ❌ No |
| Syntax | {} or set() |
| Used for | Fast lookup, unique items, mathematical operations |
