write a python program to find maximum and minimum value in set

Sets are a fundamental data structure in Python that allow storing a collection of unique elements. Python provides built-in functions and methods to perform various operations on sets efficiently. 

In this blog, we will focus on finding the maximum and minimum values in a set using Python. 

We will guide you through the process, starting with accepting user input for the set length, followed by collecting the set items from the user, and finally, we will demonstrate how to find the minimum and maximum values from the set.

Title : write a python program to find maximum and minimum value in set

Accepting User Input and Creating the Set


To begin, we need to interact with the user to obtain the necessary input for creating the set. We will utilize the input() function to prompt the user to enter the length of the set. Let's take a look at the code snippet:


set_length = int(input("Enter the length of the set: "))
set_items = set()


In the code above, we use the input() function to obtain user input for the set length. The value is converted to an integer using the int() function and stored in the variable set_length. We also create an empty set called set_items using the set() function.



Collecting Set Items from the User


Next, we will use a loop to collect the set items from the user based on the specified set length. Let's add the following code snippet:




for i in range(set_length):
    item = input("Enter item {0}: ".format(i+1))
    set_items.add(item)
 



In the code above, we iterate over a range using a for loop, starting from 0 up to set_length. Within each iteration, we prompt the user to enter an item for the set and assign it to the variable item. We then add the item to the set using the add() method.




Finding the Minimum and Maximum Values


Now that we have collected the set items from the user, we can proceed to find the minimum and maximum values. Python provides two built-in functions, min() and max(), which can be used to obtain these values. Here's the code snippet:



min_value = min(set_items)
max_value = max(set_items)

print("Minimum value: ", min_value)
print("Maximum value: ", max_value)
 




In the code above, we use the min() function to find the minimum value in the set and assign it to the variable min_value. Similarly, we use the max() function to find the maximum value and assign it to the variable max_value. Finally, we print both the minimum and maximum values using the print() function.



Final Code


set_length = int(input("Enter the length of the set: "))
set_items = set()

for i in range(set_length):
    item = input("Enter item {0}: ".format(i+1))
    set_items.add(item)

min_value = min(set_items)
max_value = max(set_items)

print("Minimum value: ", min_value)
print("Maximum value: ", max_value)





Final Code That Does Not Use Built in Function


set_length = int(input("Enter the length of the set: "))
set_items = set()

for i in range(set_length):
item = input("Enter item {0}: ".format(i+1))
set_items.add(item)

# Initialize min_value and max_value with the first item in the set
min_value = next(iter(set_items))
max_value = next(iter(set_items))

# Iterate through the set and update min_value and max_value
for item in set_items:
if item < min_value:
min_value = item
if item > max_value:
max_value = item

print("Minimum value: ", min_value)
print("Maximum value: ", max_value)


 

Output 1:



Enter the length of the set: 5
Enter item 1: 10
Enter item 2: 5
Enter item 3: 7
Enter item 4: 3
Enter item 5: 9
Minimum value:  3
Maximum value:  10
 





Output 2:



Enter the length of the set: 4
Enter item 1: 42
Enter item 2: 13
Enter item 3: 27
Enter item 4: 19
Minimum value:  13
Maximum value:  42
 




Conclusion:


In this blog, we explored how to find the maximum and minimum values in a set using Python. We started by accepting user input for the set length and collected the set items based on the specified length.

Then, we used the min() and max() functions to find the minimum and maximum values, respectively. By following these steps, we can effortlessly determine the extreme values in a set. 

We hope this guide has been helpful in expanding your knowledge of Python sets and their manipulation. Happy coding!


Post a Comment

0 Comments