30. Problem to check if a set is a superset

In this Python set program, we will check whether the given set is a superset of another set 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.

Superset: If there are two sets A and B, then set A is considered as the superset of B, if all the elements of set B are the elements of set A

Set is a superset:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use issuperset to check whether the first set is a superset of second set.
4. Print the output.

				
					a = {1,2,4,5}
b = {4,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("A is superset of B: ",a.issuperset(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {1, 4}
A is superset of B:  True
				
			

Related Articles

find the symmetric difference of two sets using the “^” operator

find the common elements between two sets.

29. Problem to find the symmetric difference between two sets

In this Python set program, we will find the symmetric difference between two sets using the “^” operator 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.

Symmetric of sets: The symmetric difference of set A and B is the set of elements which are in either of the sets A and B, but not in their intersection.

Symmetric difference between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use “^” to find the symmetric difference between sets using the “^” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {4,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Symmetric difference of two sets using the “^” operator: ",a^b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {1, 4}
Symmetric difference of two sets using the “^” operator:  {2, 5}
				
			

Related Articles

find the union of multiple sets using the | operator.

check if a set is a superset of another set.

28. Problem to find the union of multiple sets

In this Python set program, we will find the union of multiple sets by using the | operator 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.

Union of sets: Union is the combination of all the elements from more than one sets.

Union of multiple sets:

Steps to solve the program

1. Create multiple sets using {}.
2. Add some elements in the set.
3. Use “|” to find the union of sets using the “|” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8}
c={6,10,0}
print("Original set1: ",a)
print("Original set2: ",b)
print("Original set3: ",c)
print("Union of multiple sets using the | operator : ",a|b|c)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 7}
Original set3:  {0, 10, 6}
Union of multiple sets using the | operator :  {0, 1, 2, 4, 5, 6, 7, 8, 10}
				
			

Related Articles

find the intersection between two sets using the “&” operator.

find the symmetric difference of two sets using the “^” operator

27. Problem to find the intersection between two sets

In this Python set program, we will find the difference between two sets 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.

Intersection of two sets: The intersection is common elements between the two sets.

Intersection between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use “&” to find the intersection between sets using the “&” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {2,4}
print("Original set1: ",a)
print("Original set2: ",b)
print("Intersection between two sets using the “&” operator: ",a&b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
Intersection between two sets using the “&” operator:  {2, 4}
				
			

Related Articles

find the difference between two sets using the “-” operator.

find the union of multiple sets using the | operator.

26. Problem to find difference between two sets

In this Python set program, we will find the difference between two sets 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.

Difference of two sets: The difference between sets A and B is the set of all elements of A that are not elements of B.

Difference between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Subtract the second set from the first set to find the difference between sets using the “-” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {2,4}
print("Original set1: ",a)
print("Original set2: ",b)
print("Difference between two sets using - operator: ",a-b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
Difference between two sets using - operator:  {1, 5}
				
			

Related Articles

remove a random element from a set.

find the intersection between two sets using the “&” operator.

25. Problem to remove a random element from a set

In this Python set program, we will remove a random element from a set 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.

Remove a random element from a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Remove a random element from a set using the pop() function.
4. Print the set to see the result.

				
					a = {1,2,4,5}
print("Original set: ",a)
print("Remove element from set: ",a.pop())
print(a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5}
Remove element from set:  1
{2, 4, 5}
				
			

Related Articles

remove all elements from a set.

find the difference between two sets using the “-” operator.

24. Problem to remove all elements from a set

In this Python set program, we will remove all elements from a set 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.

Remove all elements from a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Remove all elements from a set using clear() function.
4. Print the set to check the output.

				
					a = {1,2,4,5}
print("Original set: ",a)
a.clear()
print(a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5}
set()
				
			

Related Articles

remove a random element from a set.

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.

20. Problem to check if elements in a set are prime

In this Python set program, we will check whether all elements in a set are prime numbers 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.

Elements in a set are prime number:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create an empty list
4. Use a for loop to iterate over elements in the set.
5. Create a count variable and assign its value equal to 0.
6. Use a nested for loop to iterate over the number from 2 to the loop number.
7. Check whether the loop number is divided by any number from the above loop.
8. If yes then add 1 to the count variable.
9. After the second loop is complete check whether the value of the count variable is equal to 0 for the first loop number.
10. If yes then add that number to the empty list.
11. At the end check whether the length of the list is equal to the length of the set using an if-else statement.
12. If yes then all elements in a set are prime numbers, else not all elements in a set are prime numbers.
13. Print the respective output.

				
					Set = {1,2,3,4,5}
prime = []
print("Original set1: ",Set)
for ele in Set:
    count = 0
    for num in range(2,ele):
        if ele%num == 0:
            count +=1
    if count == 0:
        prime.append(ele)
if len(prime) == len(Set):
    print("All elements in the set are prime numbers")
else:
    print("All elements in the set are not prime number")
				
			

Output :

				
					Original set1:  {1, 2, 3, 4, 5}
All elements in the set are not prime number
				
			

Related Articles

check if all elements in a set are odd.

check if a set is a proper subset of another set.

19. Problem to check if all elements in a set are odd

In this Python set program, we will check whether the all elements in a set are odd 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.

Elements in a set are odd:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a variable count and assign its value equal to 0.
4. Use a for loop to iterate over elements in the set.
5. Use an if statement inside the loop to check whether an element is not divisible by 2.
6. If yes then add 1 to the count variable.
7. Use an if-else statement to check whether the value of count variable is equal to the length of the set.
8. If yes then all elements in a set are odd,else not.
9. Print the respective output.

				
					Set = {1,2,3,4,5}
print("Original set1: ",Set)
count = 0
for ele in Set:
    if ele%2 != 0:
        count += 1
if count == len(Set):
    print("All elements in the set are odd")
else:
    print("All elements in the set are not odd")
				
			

Output :

				
					Original set1:  {1, 2, 3, 4, 5}
All elements in the set are not odd
				
			

Related Articles

check if all elements in a set are even.

check if all elements in a set are prime.