21. Problem to check if a set is a subset of any set

In this Python set program, we will check if a set is a subset of any set/ the set is a proper subset 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.

What is the proper subset? : A proper subset is one that contains a few elements of the original set

Set is a subset of any set:

Steps to solve program

1. Create two sets using {}.
2. Add some elements in the set.
3. Create a count variable and assign its value equal to 0.
4. Use for loop to iterate over elements in the second set.
5. Use an if statement to check whether the element is in the first set.
6. If yes then add 1 to the count variable.
7. At the end check whether the value of count variable is equal to the length of the second set.
8. If yes then print True, else print False.

				
					a = {1,2,4,5}
b = {2,4}
print("Original set1: ",a)
print("Original set2: ",b)
count = 0
for ele in b:
    if ele in a:
        count +=1
if count == len(b):
    print("True")
else:
    print("False")
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
True
				
			

Related Articles

check if all elements in a set are prime.

Leave a Comment