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.

10. Problem to show one set is a subset of another

In this Python set program, we will show one set is a subset 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.

Subset: A subset is a set that contains some elements of the original set.

One set is a subset of another:

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 one set is a subset of another set using issubset() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {2,4}
print("Original set1: ",a)
print("Original set2: ",b)
print("b is subset of a: ",b.issubset(a))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
b is subset of a:  True
				
			

Method 2 : Using Logic

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Create 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 count variable is equal to the length of the second set.
8. If yes then one set is a subset of another, else not.
9. Print the respective output.

				
					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("b is subset of a")
else:
    print("b is not a subset of a")
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
b is subset of a
				
			

Related Articles

find the symmetric difference of two sets.

check if two sets are disjoint.

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.

7. Problem to find the intersection of two sets

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

Steps to solve the program

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

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

Output :

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

find the union of two sets.

find the difference of two sets.

6. Problem to find the union of two sets

In this Python set program, we will find the union of two sets using Python 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 two sets: Union is the combination of all the elements from both sets.

Union of two sets:

Steps to solve the program

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

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

Output :

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

check if an element is present in a set.

Find the intersection of two sets.

5. Problem to check if an element is in a set

In this python set program, we will check if an element is in a set and if yes then we will print True, else print False 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 an element is in a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Use an if-else statement to check whether 5 is in the set or not.
4. Print the respective output.

				
					a = {1,2,3,5,6}
print("Original set: ",a)
if 5 in a:
    print("True")
else:
    print("False")
				
			

Output :

				
					Original set:  {1, 2, 3, 5, 6}
True
				
			

Find the length of a set.

find the union of two sets.