Write a Python program to remove all elements from a given set


This program demonstrates how to clear or remove all elements from a set in Python. The code first declares a set color containing various colors as strings. The set is then printed to show its original values and the type of the set. Next, the clear() method is used to remove all elements from the set color. The set is then printed again to show that it's now an empty set. In this example, the clear() method removes all elements from the set color, and the set is now empty. The type of the set remains unchanged, which is set.

Source Code

color = {"Red","Green","Pink","White","Black","Yellow","Blue"}
print(color)
print(type(color))
 
#Remove All Elements from given set
color.clear()
print("\nAfter Remove all Elements give Sets :",color)
print(type(color))

Output

{'Red', 'Green', 'Black', 'Pink', 'White', 'Yellow', 'Blue'}
<class 'set'>

After Remove all Elements give Sets : set()
<class 'set'>