5 3 Sets Explained
Key Concepts
Sets in Python are unordered collections of unique elements. The key concepts include:
- Creating Sets
- Adding and Removing Elements
- Set Operations
- Set Methods
- Practical Applications
1. Creating Sets
Sets are created using curly braces {} or the set() constructor. Sets do not allow duplicate values.
Example:
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
empty_set = set()
print(empty_set) # Output: set()
2. Adding and Removing Elements
Elements can be added to a set using the add() method and removed using the remove() or discard() methods.
Example:
unique_numbers = {1, 2, 3, 4, 5}
unique_numbers.add(6)
print(unique_numbers) # Output: {1, 2, 3, 4, 5, 6}
unique_numbers.remove(3)
print(unique_numbers) # Output: {1, 2, 4, 5, 6}
unique_numbers.discard(7) # No error if element is not found
print(unique_numbers) # Output: {1, 2, 4, 5, 6}
3. Set Operations
Sets support various mathematical operations such as union, intersection, difference, and symmetric difference.
Example:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
union_set = set1 | set2
print(union_set) # Output: {1, 2, 3, 4, 5, 6}
intersection_set = set1 & set2
print(intersection_set) # Output: {3, 4}
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}
symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set) # Output: {1, 2, 5, 6}
4. Set Methods
Python provides several built-in methods to manipulate sets. Some common methods include union(), intersection(), difference(), and symmetric_difference().
Example:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5, 6}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3, 4}
difference_set = set1.difference(set2)
print(difference_set) # Output: {1, 2}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 5, 6}
5. Practical Applications
Sets are useful for tasks that require unique elements, such as removing duplicates from a list, checking for membership, and performing set operations.
Example:
# Removing duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers) # Output: [1, 2, 3, 4, 5]
# Checking for membership
fruits = {"apple", "banana", "cherry"}
print("banana" in fruits) # Output: True
print("orange" in fruits) # Output: False
Putting It All Together
By understanding and using sets effectively, you can efficiently manage and manipulate collections of unique elements in Python.
Example:
unique_numbers = {1, 2, 3, 4, 5}
unique_numbers.add(6)
unique_numbers.remove(3)
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)
symmetric_difference_set = set1.symmetric_difference(set2)
print(union_set) # Output: {1, 2, 3, 4, 5, 6}
print(intersection_set) # Output: {3, 4}
print(difference_set) # Output: {1, 2}
print(symmetric_difference_set) # Output: {1, 2, 5, 6}