w3resource

Python: Find maximum and the minimum value in a set

Python sets: Exercise-14 with Solution

Write a Python program to find the maximum and minimum values in a set.

Visual Presentation:

NumPy Sets: Find maximum and the minimum value in a set.

Sample Solution:

Python Code:

# Create a set 'setn' with elements 5, 10, 3, 15, 2, and 20.
setn = {5, 10, 3, 15, 2, 20}

# Print a message to indicate the original set elements.
print("Original set elements:")

# Print the contents of 'setn'.
print(setn)

# Print the type of 'setn'.
print(type(setn))

# Print a message to indicate finding the maximum value in the set.
print("\nMaximum value of the said set:")

# Use the 'max()' function to find and print the maximum value in 'setn'.
print(max(setn))

# Print a message to indicate finding the minimum value in the set.
print("\nMinimum value of the said set:")

# Use the 'min()' function to find and print the minimum value in 'setn'.
print(min(setn)) 

Sample Output:

Original set elements:
{2, 3, 5, 10, 15, 20}
<class 'set'>

Maximum value of the said set:
20

Minimum value of the said set:
2 

Python Code Editor:

Previous: Write a Python program to use of frozensets.
Next: Write a Python program to find the length of a set.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/sets/python-sets-exercise-14.php