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.

18. Problem to check if all elements in a set are even

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

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 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 even, 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 even")
else:
    print("All elements in the set are not even")
				
			

Output :

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

Related Articles

find the average of elements in a set.

check if all elements in a set are odd.

17. Problem to find the average of elements in a set

In this Python set program, we will find the average of elements in 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.

Average of elements in a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a variable total and assign its value equal to 0.
4. Use a for loop to iterate over elements in the set.
5. During iteration add element to the total variable to get the sum of elements in a set.
6. Now divide the total by length of the set to get the average of elements in a set.
7. Use len() function for this purpose.
8. Print the output.

				
					Set = {1,2,3,4,5}
print("Original set1: ",Set)
total = 0
for ele in Set:
    total += ele
print("Average of the set: ",total/len(Set))
				
			

Output :

				
					Original set1:  {1, 2, 3, 4, 5}
Average of the set:  3.0
				
			

Related Articles

find the sum of elements in a set.

check if all elements in a set are even.

16. Problem to find the sum of elements in a set

In this Python set program, we will find the sum of elements in 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.

Sum of elements in a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a variable total and assign its value equal to 0.
4. Use a for loop to iterate over elements in the set.
5. During iteration add element to the total variable to get the sum of elements in a set.
6. Print the output.

				
					Set = {1,2,3,4,5}
total = 0
print("Original set1: ",Set)
for ele in Set:
    total += ele
print("Total of elements in the set: ",total)
				
			

Output :

				
					Original set1:  {1, 2, 3, 4, 5}
Total of elements in the set:  15
				
			

Related Articles

find the minimum element in a set.

find the average of elements in a set.

15. Problem to find the minimum element in a set

In this Python set program, we will find the minimum element in 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.

Minimum element in a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Get the minimum element in a set using min() function.

				
					Set = {10,23,45,66,96,83}
print("Original set1: ",Set)
print("Minimum value: ",min(Set))
				
			

Output :

				
					Original set1:  {96, 66, 10, 45, 83, 23}
Minimum value:  10
				
			

Related Articles

find the maximum element in a set.

find the sum of elements in a set.