DEV Community

Haripriya V
Haripriya V

Posted on

TASK – The Botanical Garden and Rose Garden – Python SETS

1.Create a set named rose_garden containing different types of roses: "red rose", "white rose", "yellow rose". Print the same.

CODE:

rose_garden = {"red rose", "white rose", "yellow rose"}
print(rose_garden)

OUTPUT:

{'red rose', 'white rose', 'yellow rose'}

EXPLANATION:

  • A set stores unique elements

  • Order may vary

2.Add "pink rose" to the rose_garden set. Print the set to confirm the addition.

CODE:

rose_garden.add("pink rose")
print(rose_garden)

OUTPUT:

{'red rose', 'white rose', 'yellow rose', 'pink rose'}

EXPLANATION:

-add() inserts a new element

3.Remove "yellow rose" from the rose_garden set using the remove() method. Print the set to verify the removal.

CODE:

rose_garden.remove("yellow rose")
print(rose_garden)

OUTPUT:

{'red rose', 'white rose', 'pink rose'}

EXPLANATION:

  • remove() deletes element (error if not present)

4.Create another set botanical_garden with elements "sunflower", "tulip", and "red rose". Find the union of rose_garden and botanical_garden and print the result.

CODE:

botanical_garden = {"sunflower", "tulip", "red rose"}
print(rose_garden.union(botanical_garden))

OUTPUT:

{'red rose', 'white rose', 'pink rose', 'sunflower', 'tulip'}

EXPLANATION:

-union() combines both sets

5.Find the intersection of rose_garden and botanical_garden and print the common elements.

CODE:

print(rose_garden.intersection(botanical_garden))

OUTPUT:

{'red rose'}

EXPLANATION:

-Common elements

6.Find the difference between rose_garden and botanical_garden and print the elements that are only in rose_garden.

CODE:

print(rose_garden.difference(botanical_garden))

OUTPUT:

{'white rose', 'pink rose'}

EXPLANATION:

-Elements only in first set

7.Find the symmetric difference between rose_garden and botanical_garden and print the elements unique to each set.

CODE:

print(rose_garden.symmetric_difference(botanical_garden))

OUTPUT:

{'white rose', 'pink rose', 'sunflower', 'tulip'}

EXPLANATION:

-Unique elements from both sets

8.Create a set small_garden containing "red rose", "white rose". Check if small_garden is a subset of rose_garden and print the result.

CODE:

small_garden = {"red rose", "white rose"}
print(small_garden.issubset(rose_garden))

OUTPUT:

True

EXPLANATION:

  • Checks if all elements exist in another set

9.Check if rose_garden is a superset of small_garden and print the result.

CODE:

print(rose_garden.issuperset(small_garden))

OUTPUT:

True

EXPLANATION:

-Opposite of subset

10.Use the len() function to find the number of elements in the rose_garden set. Print the result.

CODE:

print(len(rose_garden))

OUTPUT:

3

EXPLANATION:

-Counts elements

11.Use the discard() method to remove "pink rose" from the rose_garden set. Try to discard a non-existent element "blue rose" and observe what happens.

CODE:

rose_garden.discard("pink rose")
rose_garden.discard("blue rose")
print(rose_garden)

OUTPUT:

{'red rose', 'white rose'}

EXPLANATION:

-discard() doesn’t raise error if item missing

12.Use the clear() method to remove all elements from the rose_garden set. Print the set to confirm it’s empty.

CODE:

rose_garden.clear()
print(rose_garden)

OUTPUT:

set()

EXPLANATION:

-Removes all elements

13.Make a copy of the botanical_garden set using the copy() method. Add "lily" to the copy and print both sets to see the differences.

CODE:

`copy_set = botanical_garden.copy()
copy_set.add("lily")

print(botanical_garden)
print(copy_set)`

OUTPUT:

{'sunflower', 'tulip', 'red rose'}
{'sunflower', 'tulip', 'red rose', 'lily'}

EXPLANATION:

-Copy creates separate set

14.Create a frozen set immutable_garden with elements "orchid", "daisy", "red rose". Try to add or remove an element and observe what happens.

CODE:

immutable_garden = frozenset(["orchid", "daisy", "red rose"])
print(immutable_garden)

OUTPUT:

frozenset({'orchid', 'daisy', 'red rose'})

EXPLANATION:

-frozenset is immutable (cannot modify)

15.Iterate over the botanical_garden set and print each element.

CODE:

for flower in botanical_garden:
print(flower)

OUTPUT:

sunflower
tulip
red rose

EXPLANATION:

-Loop through elements

16.Use set comprehension to create a set even_numbers containing even numbers from 1 to 10.

CODE:

even_numbers = {x for x in range(1, 11) if x % 2 == 0}
print(even_numbers)

OUTPUT:

{2, 4, 6, 8, 10}

EXPLANATION:

-Creates set using condition

17.Given a list of flowers ["rose", "tulip", "rose", "daisy", "tulip"], use a set to remove duplicates and print the unique flowers.

CODE:

flowers = ["rose", "tulip", "rose", "daisy", "tulip"]
unique = set(flowers)
print(unique)

OUTPUT:

{'rose', 'tulip', 'daisy'}

EXPLANATION:

-Sets automatically remove duplicates

18.Check if "sunflower" is in the botanical_garden set and print the result.

CODE:

print("sunflower" in botanical_garden)

OUTPUT:

True

EXPLANATION:

-in checks existence

19.Use the intersection_update() method to update the botanical_garden set with only the elements found in rose_garden. Print the updated set.

CODE:

botanical_garden.intersection_update({"red rose"})
print(botanical_garden)

OUTPUT:

{'red rose'}

EXPLANATION:

-Keeps only common elements

20.Use the difference_update() method to remove all elements in small_garden from botanical_garden. Print the updated set.

CODE:

`botanical_garden = {"sunflower", "tulip", "red rose"}
small_garden = {"red rose"}

botanical_garden.difference_update(small_garden)
print(botanical_garden)`

OUTPUT:

{'sunflower', 'tulip'}

EXPLANATION:

-Removes specified elements

Top comments (0)