Join Regular Classroom : Visit ClassroomTech

Python Set – clear() Method

It is a method in python that clears the given set and leaves a blank set. It doesn’t return any value and it modifies the given set.

Syntax:
set.clear()

Also Read: Python Set – pop() Method

Example:

# Python code to understand clear() method in set
# www.codewindow.in

s = {"amazing", "code"}
s1 = {"window", "hustle"}
s.clear()
s1.clear()
print(s)
print(s1)

Output:

set()
set()

Explanation:
Here all the elements in both the sets are cleared and left a blank set.

Example 2:

# Python code to understand clear() method in set
# www.codewindow.in

s = {5, 8, 4, 78, 45, 85}
s.clear()
print(s)

Output:

set()

Explanation:
Here all the elements in the set are cleared and left a blank set.


Follow Us

You Missed

Also Checkout