- Create a set named rose_garden and print it rose_garden = {"red rose", "white rose", "yellow rose"} print(rose_garden)
Explanation:
Sets are unordered collections of unique elements.
Duplicate values are not allowed.
- Add "pink rose" to the set rose_garden.add("pink rose") print(rose_garden)
Explanation:
add() inserts a new element into the set.
- Remove "yellow rose" using remove() rose_garden.remove("yellow rose") print(rose_garden)
Explanation:
remove() deletes a specific element.
It raises an error if the element does not exist.
- Create another set botanical_garden and find union botanical_garden = {"sunflower", "tulip", "red rose"}
union_set = rose_garden.union(botanical_garden)
print("Union:", union_set)
Explanation:
Union combines all unique elements from both sets.
- Find intersection intersection_set = rose_garden.intersection(botanical_garden) print("Intersection:", intersection_set)
Explanation:
Intersection returns common elements.
- Find difference (only in rose_garden) difference_set = rose_garden.difference(botanical_garden) print("Difference:", difference_set)
Explanation:
Difference returns elements present only in the first set.
- Find symmetric difference symmetric_diff = rose_garden.symmetric_difference(botanical_garden) print("Symmetric Difference:", symmetric_diff)
Explanation:
Symmetric difference returns elements unique to each set.
- Check subset small_garden = {"red rose", "white rose"} print("Is subset:", small_garden.issubset(rose_garden))
Explanation:
issubset() checks if all elements exist in another set.
- Check superset print("Is superset:", rose_garden.issuperset(small_garden))
Explanation:
issuperset() checks if a set contains all elements of another set.
- Find number of elements print("Number of elements:", len(rose_garden))
Explanation:
len() returns total elements in the set.
- Use discard() method rose_garden.discard("pink rose") rose_garden.discard("blue rose") # Does not exist print(rose_garden)
Explanation:
discard() removes an element if it exists.
It does NOT raise an error if the element is missing.
- Use clear() method rose_garden.clear() print(rose_garden)
Explanation:
clear() removes all elements from the set.
- Copy set and modify copy botanical_copy = botanical_garden.copy() botanical_copy.add("lily")
print("Original:", botanical_garden)
print("Copy:", botanical_copy)
Explanation:
copy() creates a separate copy.
Changes to the copy do not affect the original.
- Create a frozen set immutable_garden = frozenset({"orchid", "daisy", "red rose"}) print(immutable_garden)
The following lines would cause an error:
immutable_garden.add("lily")
immutable_garden.remove("orchid")
Explanation:
frozenset is immutable.
Elements cannot be added or removed.
- Iterate over botanical_garden for flower in botanical_garden: print(flower)
Explanation:
Loop through set elements using for.
- Set comprehension for even numbers even_numbers = {num for num in range(1, 11) if num % 2 == 0} print(even_numbers)
Explanation:
Set comprehension creates sets dynamically.
Only even numbers are included.
- Remove duplicates from a list using set flowers = ["rose", "tulip", "rose", "daisy", "tulip"] unique_flowers = set(flowers)
print(unique_flowers)
Explanation:
Converting a list to a set removes duplicates automatically.
- Check membership print("sunflower" in botanical_garden)
Explanation:
in checks if element exists in the set.
Returns True or False.
- Use intersection_update() botanical_garden.intersection_update(rose_garden) print("After intersection_update:", botanical_garden)
Explanation:
Keeps only common elements.
Updates the original set.
- Use difference_update() botanical_garden.difference_update(small_garden) print("After difference_update:", botanical_garden)
Explanation:
Removes elements found in another set.
Updates the original set.
Top comments (0)