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.

14. Problem to find the maximum element in a set

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

Maximum element in a set:

Method 1 : Using Logic

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a variable maximum and assign its value equal to 0.
4. Use a for loop to iterate over elements in the set.
5. If the value of the element is greater than value of the maximum variable then assign that value to the maximum variable.
6. Print the output to get the maximum element in a set.

				
					Set = {10,23,45,66,96,83}
print("Original set1: ",Set)
maximum = 0
for ele in Set:
    if ele > maximum:
        maximum = ele
print("Maximum value: ",maximum)
				
			

Output :

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

Method 2 : Using in-built function.

Steps to solve the program

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

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

Output :

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

Related Articles

convert a set to a list.

find the minimum element in a set.

13. Problem to convert a set to a list

In this Python set program, we will convert a set to a list 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.

Convert a set to a list:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Convert a set to a list using list() function.
4. Print the output.

				
					Set = {1,2,3,4,5}
set_list = list(List)
print("Original set: ",Set)
print("set to list: ",set_list)
				
			

Output :

				
					Original set:  {1, 2, 3, 4, 5}
set to list:  [1, 2, 3, 4, 5]
				
			

Related Articles

convert a list to a set.

find the maximum element in a set.

12. Problem to convert list to a set

In this Python set program, we will convert list to 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.

Convert list to a set:

Steps to solve the program

1. Create a list using [].
2. Add some elements in the list.
3. Convert a list to a set using set() function.
4. Print the output.

				
					List = [1,2,3,4,5]
list_set = set(List)
print("Original list: ",List)
print("List to set: ",list_set)
				
			

Output :

				
					Original list:  [1, 2, 3, 4, 5]
List to set:  {1, 2, 3, 4, 5}
				
			

Related Articles

check if two sets are disjoint.

convert a set to a list.

11. Problem to check if two sets are disjoint

In this Python set program, we will check whether two sets are disjoint 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.

Disjoint set: A pair of sets that do not have any common element are called disjoint sets.

Two sets are disjoint:

Method 1 : Using in-built function

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Check if two sets are disjoint using isdisjoint() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8,9}
print("Original set1: ",a)
print("Original set2: ",b)
print("Are two sets dis-joint: ",a.isdisjoint(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 7}
Are two sets dis-joint:  True
				
			

Method 2 : Using Logic

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Create a count variable and assign its value equal to 0.
4. Use a 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. Use an if-else statement to check whether the value of the count variable is greater than 0.
8. If no then two sets are disjoint, else yes.
9. Print the respective output.

				
					a = {1,2,4,5}
b = {7,8,9}
print("Original set1: ",a)
print("Original set2: ",b)
count = 0
for ele in b:
    if ele in a:
        count+=1
if count > 0:
    print("Are two sets dis-joint:  False")
else:
    print("Are two sets dis-joint:  True")
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 7}
Are two sets dis-joint:  True
				
			

Related Articles

show if one set is a subset of another set.

convert a list to a set.

9. Problem to find symmetric difference of two sets

In this Python set program, we will find the symmetric difference of two sets.

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 difference of two 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 of two sets: 

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Get the symmetric difference of sets using the  symmetric_difference() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8,9,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Symmetric difference  of a and b: ",a.symmetric_difference(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 1, 7}
Symmetric difference  of a and b:  {2, 4, 5, 7, 8, 9}
				
			

Related Articles

find the difference of two sets.

show if one set is a subset of another set.

8. Problem to find the difference of two sets

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

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Get the difference of two using the difference() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8,9,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Difference of a and b:",a-b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 1, 7}
Difference of a and b:  {2, 4, 5}
				
			

find the intersection of two sets.

find the symmetric difference of two sets.