Python Program for Set min() Method

In the previous article, we have discussed Python oct() Function with Examples
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given Set = {1, 0, 5, 8, 2, 8, 6, 2}

Output:

The given set is :
{0, 1, 2, 5, 6, 8}
The above Given set's minimum value =  0

Example2:

Input:

Given Set = {-1, 5, 3, 10, 16, 0, 3, 2, 5}

Output:

The given set is :
{0, 2, 3, 5, 10, 16, -1}
The above Given set's minimum value =  -1

Program for Set min() Method in Python

Set min() Function:

The set min function in Python is used to find the smallest value in a given set.

Syntax:

min(set_name)

The set min function finds the smallest number of items from a given set’s total number of items.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply the min() method to the given set to get the minimum value in the given set.
  • Store it in another variable.
  • Print the minimum value in the above-given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {1, 0, 5, 8, 2, 8, 6, 2}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply min() method to the given set to get the minimum value in the given set.
# Store it in another variable.
minim_val = min(gven_set)
# Print the minimum value in the above-given set.
print("The above Given set's minimum value = ", minim_val)

Output:

The given set is :
{0, 1, 2, 5, 6, 8}
The above Given set's minimum value =  0

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply the min() method to the given set to get the minimum value in the given set.
  • Store it in another variable.
  • Print the minimum value in the above-given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply min() method to the given set to get the minimum value in the given set.
# Store it in another variable.
minim_val = min(gven_set)
# Print the minimum value in the above-given set.
print("The above Given set's minimum value = ", minim_val)

Output:

Enter some random values separated by spaces = -10 3 4 -25 60 50 3 -10
The given set is :
{3, 4, -25, 50, -10, 60}
The above Given set's minimum value = -25