๐ Lesson 10: Python Sets – Unique Collections Made Simple
Welcome to Lesson 10! Today we’ll learn about sets — a Python data structure that stores unique items without any particular order. Sets are useful when you want to avoid duplicates and perform fast membership tests.
⭐ What You Will Learn in This Lesson
- How to create and access sets
- Adding and removing items in sets
- Using set operations like union, intersection, and difference
- Practical use cases for unique collections
๐ฅ Who Is This Lesson For?
- Beginners learning Python data structures
- Students who want to manage unique items efficiently
- Anyone dealing with duplicates in lists or performing mathematical operations
๐ฆ What Is a Set?
A set is a collection of unique items enclosed in curly braces {}. Items in a set are unordered and duplicates are automatically removed.
my_set = {"apple", "banana", "cherry"}
print(my_set)
๐ง Output (order may vary)
{'apple', 'banana', 'cherry'}
๐งฉ 1. Adding Items
- add() – adds a single item
- update() – adds multiple items at once
my_set.add("orange")
my_set.update(["kiwi", "mango"])
print(my_set)
๐งฉ 2. Removing Items
- remove(item) – removes an item, throws error if not found
- discard(item) – removes an item, no error if not found
- pop() – removes a random item
- clear() – removes all items
my_set.remove("kiwi")
my_set.discard("pineapple") # no error even if not in set
print(my_set)
๐งฉ 3. Set Operations
Sets are great for math-like operations:
- union() – combines sets
- intersection() – items common to both sets
- difference() – items in first set not in second
- symmetric_difference() – items in one set or the other, but not both
A = {1, 2, 3}
B = {2, 3, 4}
print(A.union(B)) # {1, 2, 3, 4}
print(A.intersection(B)) # {2, 3}
print(A.difference(B)) # {1}
print(A.symmetric_difference(B))# {1, 4}
๐งฉ 4. Why Use Sets?
- Store unique items only
- Fast membership tests
- Useful for math and logic operations
- Remove duplicates from lists easily
๐งช Practice
- Create a set of your favorite fruits (include duplicates to see them removed).
- Add two more fruits using
add()andupdate(). - Create another set of fruits and find:
- Union
- Intersection
- Difference
- Symmetric Difference
- Remove a fruit using
discard()and print the set.
Bonus: Try combining two sets and removing duplicates automatically.
❌ Common Mistakes
- Trying to access items by index (sets are unordered)
- Using
remove()without checking if the item exists - Expecting duplicate items to remain (sets automatically remove duplicates)
❓ Frequently Asked Questions (FAQ)
1. Can sets contain duplicate items?
No. Sets automatically remove duplicates.
2. Are sets ordered?
No. Items in a set have no particular order.
3. Can sets store different data types?
Yes. Sets can contain numbers, strings, or mixed types as long as they are immutable.
4. How do sets differ from lists?
Sets store only unique items, while lists can have duplicates and maintain order.
๐ What’s Next?
In the next lesson, you’ll learn about:
- String methods
- Manipulating text efficiently in Python
- Practical string operations for programs
Comments
Post a Comment