w3resource

Python: Remove all elements from a given set

Python sets: Exercise-12 with Solution

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

Visual Presentation:

NumPy Sets: Remove all elements from a given set.

Sample Solution:

Python Code:

# Create a set 'setc' with elements "Red", "Green", "Black", and "White".
setc = {"Red", "Green", "Black", "White"}

# Print a message to indicate the original set elements.
print("Original set elements:")

# Print the contents of 'setc'.
print(setc)

# Print a message to indicate the removal of all elements from the set.
print("\nAfter removing all elements of the said set.")

# Use the 'clear()' method to remove all elements from 'setc'.
setc.clear()

# Print 'setc' after clearing, which will be an empty set.
print(setc) 

Sample Output:

Original set elements:
{'Green', 'Black', 'Red', 'White'}

After removing all elements of the said set.
set()

Python Code Editor:

Previous: Write a Python program to create a shallow copy of sets.
Next: Write a Python program to use of frozensets.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/sets/python-sets-exercise-12.php