Program for Set clear() Method

Python Program for Set clear() Method

Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The given set is :
{'this', 'btechgeeks', 'is', 'hello'}
The given set after clearing all the values :
set()

Explanation:

Here all the values of the given set got cleared using the clear() function.

Example2:

Input:

Given set = {'good', 'morning', 'btechgeeks'}

Output:

The given set is :
{'morning', 'btechgeeks', 'good'}
The given set after clearing all the values :
set()

Explanation:

Here all the values of the given set got cleared using the clear() function.

Program for Set clear() Method in Python

Set clear() Method:

One of the set methods used to clear or remove total set items in Python is the set clear() function.

Syntax:

set_name.clear()

The set clear function allows you to clear or remove all of the items in a given set.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Clear the values of the given set by applying the clear() function to the given set.
  • Print the above-given set after clearing all the values.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Clear the values of the given set by applying clear() function to the given set.
gven_set.clear()
# Print the above-given set after clearing all the values.
print("The given set after clearing all the values :")
print(gven_set)

Output:

The given set is :
{'this', 'btechgeeks', 'is', 'hello'}
The given set after clearing all the values :
set()

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Clear the values of the given set by applying the clear() function to the given set.
  • Print the above-given set after clearing all the values.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), input(), and split() functions.
# Store it in a variable.
gven_set = set(input("Enter some random values separated by spaces = ").split())
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Clear the values of the given set by applying clear() function to the given set.
gven_set.clear()
# Print the above-given set after clearing all the values.
print("The given set after clearing all the values :")
print(gven_set)

Output:

Enter some random values separated by spaces = good morning btechgeeks
The given set is :
{'morning', 'btechgeeks', 'good'}
The given set after clearing all the values :
set()