THE BOTANICAL GARDEN AND ROSE GARDEN
The Botanical Garden and Rose Garden – Python Sets
Introduction
Sets in Python are used to store unique elements and are especially useful when we want to perform mathematical operations like union, intersection, and difference. Unlike lists, sets do not allow duplicate values. In this task, various set operations are explored to understand their behavior and use cases.
1. Creating and printing a set
rose_garden = {"red rose", "white rose", "yellow rose"}
print(rose_garden)
Approach: Set creation using curly braces
Why: Sets automatically store unique values and are ideal for grouping items without duplicates.
2. Adding an element
rose_garden.add("pink rose")
print(rose_garden)
Approach: add() method
Why: Adds a single element to the set efficiently.
3. Removing an element
rose_garden.remove("yellow rose")
print(rose_garden)
Approach: remove() method
Why: Removes a specific element.
Note: It raises an error if the element is not present.
4. Union of two sets
botanical_garden = {"sunflower", "tulip", "red rose"}
print(rose_garden.union(botanical_garden))
Approach: union() method
Why: Combines elements from both sets without duplicates.
5. Intersection of sets
print(rose_garden.intersection(botanical_garden))
Approach: intersection()
Why: Finds common elements between sets.
6. Difference between sets
print(rose_garden.difference(botanical_garden))
Approach: difference()
Why: Shows elements present in one set but not in the other.
7. Symmetric difference
print(rose_garden.symmetric_difference(botanical_garden))
Approach: symmetric_difference()
Why: Returns elements that are unique to each set.
8. Subset check
small_garden = {"red rose", "white rose"}
print(small_garden.issubset(rose_garden))
Approach: issubset()
Why: Checks if all elements of one set exist in another.
9. Superset check
print(rose_garden.issuperset(small_garden))
Approach: issuperset()
Why: Opposite of subset; checks if a set contains another.
10. Finding number of elements
print(len(rose_garden))
Approach: len() function
Why: Counts total elements in the set.
11. Using discard()
rose_garden.discard("pink rose")
rose_garden.discard("blue rose")
print(rose_garden)
Approach: discard()
Why: Unlike remove(), it does not raise an error if the element is not present.
12. Clearing the set
rose_garden.clear()
print(rose_garden)
Approach: clear()
Why: Removes all elements efficiently.
13. Copying a set
copy_set = botanical_garden.copy()
copy_set.add("lily")
print(botanical_garden)
print(copy_set)
Approach: copy() method
Why: Prevents modification of the original set.
14. Frozen set (immutable)
immutable_garden = frozenset(["orchid", "daisy", "red rose"])
# immutable_garden.add("lily") → will raise error
Approach: frozenset()
Why: Creates an immutable set that cannot be changed.
15. Iterating through a se
for item in botanical_garden:
print(item)
Approach: Looping
Why: Allows access to each element.
16. Set comprehension
even_numbers = {x for x in range(1, 11) if x % 2 == 0}
print(even_numbers)
Approach: Set comprehension
Why: Compact way to generate sets.
17. Removing duplicates using set
flowers = ["rose", "tulip", "rose", "daisy", "tulip"]
unique_flowers = set(flowers)
print(unique_flowers)
Approach: Converting list to set
Why: Automatically removes duplicates.
18. Checking membership
print("sunflower" in botanical_garden)
Approach: Membership operator
Why: Quickly checks if element exists.
19. Using intersection_update()
botanical_garden.intersection_update(rose_garden)
print(botanical_garden)
Approach: intersection_update()
Why: Updates the set directly with common elements.
20. Using difference_update()
botanical_garden.difference_update(small_garden)
print(botanical_garden)
Approach: difference_update()
Why: Removes elements present in another set directly.
Conclusion
This task demonstrates how sets are useful for handling unique data and performing mathematical operations like union, intersection, and difference. Methods like add(), discard(), and set comprehension help in writing efficient and clean code. Choosing the right method depends on whether we want to modify the original set or create a new one.
Comments
Post a Comment