35. Problem to check if a set is empty

In this Python set program, we will check if a set is empty or not with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Check if a set is empty:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. If the length of the set is equal to 0 then set is empty, else not.
4. Use an if-else statement for this purpose.

				
					a = {1, 2, 4, 5, 7, 8, 9}
print("Original set1: ",a)
if len(a) == 0:
    print("Set is empty")
else:
    print("Set is not empty")
				
			

Output :

				
					Original set1:  {1, 2, 4, 5, 7, 8, 9}
Set is not empty
				
			

Related Articles

remove multiple elements from a set.

check if two sets are equal.

Leave a Comment