DEV Community

Santhoshi Mary A
Santhoshi Mary A

Posted on

TASK – The Botanical Garden and Rose Garden – Python SETS

  1. 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.

  1. Add "pink rose" to the set rose_garden.add("pink rose") print(rose_garden)

Explanation:

add() inserts a new element into the set.

  1. 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.

  1. 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.

  1. Find intersection intersection_set = rose_garden.intersection(botanical_garden) print("Intersection:", intersection_set)

Explanation:

Intersection returns common elements.

  1. 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.

  1. 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.

  1. 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.

  1. Check superset print("Is superset:", rose_garden.issuperset(small_garden))

Explanation:

issuperset() checks if a set contains all elements of another set.

  1. Find number of elements print("Number of elements:", len(rose_garden))

Explanation:

len() returns total elements in the set.

  1. 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.

  1. Use clear() method rose_garden.clear() print(rose_garden)

Explanation:

clear() removes all elements from the set.

  1. 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.

  1. 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.

  1. Iterate over botanical_garden for flower in botanical_garden: print(flower)

Explanation:

Loop through set elements using for.

  1. 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.

  1. 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.

  1. Check membership print("sunflower" in botanical_garden)

Explanation:

in checks if element exists in the set.
Returns True or False.

  1. Use intersection_update() botanical_garden.intersection_update(rose_garden) print("After intersection_update:", botanical_garden)

Explanation:

Keeps only common elements.
Updates the original set.

  1. 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)