Welcome to Day 7 of your 180-day AI and Machine Learning journey! Today, we’ll explore sets, which are unordered collections of unique items. Sets are useful for tasks like removing duplicates and performing mathematical operations. By the end of this article, you’ll understand how to create, modify, and use sets effectively.
What Are Sets?
A set is an unordered collection of unique items. Sets are:
Unordered: Items do not have a defined order.
Mutable: You can add or remove items.
Unique: Duplicate items are not allowed.
Creating a Set
You can create a set by enclosing items in curly braces {}
or using the set()
function.
Example:
fruits = {"apple", "banana", "cherry"}
numbers = set([1, 2, 3, 4, 5])
Accessing Set Items
Since sets are unordered, you cannot access items using an index. However, you can loop through the set or check if an item exists.
Example:
fruits = {"apple", "banana", "cherry"}
for fruit in fruits:
print(fruit)
print("banana" in fruits) # Output: True
Modifying Sets
You can add or remove items from a set.
1. Adding Items
add()
: Adds a single item.update()
: Adds multiple items.
Example:
fruits = {"apple", "banana"}
fruits.add("cherry") # {'apple', 'banana', 'cherry'}
fruits.update(["orange", "mango"]) # {'apple', 'banana', 'cherry', 'orange', 'mango'}
2. Removing Items
remove()
: Removes a specific item (raises an error if the item does not exist).discard()
: Removes a specific item (does not raise an error if the item does not exist).pop()
: Removes and returns an arbitrary item.clear()
: Removes all items.
Example:
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana") # {'apple', 'cherry'}
fruits.discard("mango") # No error
fruits.pop() # Removes and returns an arbitrary item
fruits.clear() # set()
Set Operations
Sets support mathematical operations like union, intersection, and difference.
1. Union (|
)
Combines items from both sets.
Example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # {1, 2, 3, 4, 5}
2. Intersection (&
)
Returns items common to both sets.
Example:intersection_set = set1 & set2 # {3}
3. Difference (-
)
Returns items in the first set but not in the second.
Example:difference_set = set1 - set2 # {1, 2}
4. Symmetric Difference (^
)
Returns items in either set but not in both.
Example:
symmetric_difference_set = set1 ^ set2 # {1, 2, 4, 5}
Practice Exercise
Create a set of your favorite fruits and print it.
Add a new fruit to the set and print the updated set.
Remove a fruit from the set and print the updated set.
Perform a union operation on two sets and print the result.
Solution:
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'apple', 'banana', 'cherry'}
fruits.add("orange")
print(fruits) # Output: {'apple', 'banana', 'cherry', 'orange'}
fruits.remove("banana")
print(fruits) # Output: {'apple', 'cherry', 'orange'}
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set) # Output: {1, 2, 3, 4, 5}
Key Takeaways
Sets are unordered, mutable collections of unique items.
Use sets to remove duplicates and perform mathematical operations.
Use methods like
add()
,remove()
, andupdate()
to modify sets.
What’s Next?
Tomorrow, we’ll explore dictionaries, which are key-value pairs used to store and retrieve data efficiently.
Congratulations on completing Day 7! Keep practicing, and see you tomorrow! 🚀