Python min() Function with Examples

In the previous article, we have discussed Python max() Function with Examples
min() Function in Python:

The min() function returns the item with the lowest value or the item in an iterable with the lowest value.

If the values are strings, they are compared alphabetically.

Syntax:

min(iterable)

or

min(n1, n2, n3,........)

Parameters

iterable: An iterable that compares one or more items.

n1, n2, n3,……..: a single or multiple items to compare.

Return Value:

min() returns the iterable’s smallest element.

Examples:

Example1:

Input:

Given List = [1, 2, 5, 6, 15, 7]

Output:

The smallest number in the given list = 1

Example2:

Input:

Given List = ["hello", "this", "is", "btechgeeks"]

Output:

The smallest string in the given list (alphabetically ordered) is :
btechgeeks

Note:

If the items in an iterable are strings, the smallest(alphabetically ordered) 
item is returned.

min() Function with Examples in Python

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

1)For Number List

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the min() function to get the smallest element in the given list.
  • Store it in another variable.
  • Print the smallest element(number) in the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 5, 6, 15, 7]
# Pass the given list as an argument to the min() function to get the smallest
# element in the given list.
# Store it in another variable.
smallst_numb = min(gvn_lst)
# Print the smallest element(number) in the given list.
print("The smallest number in the given list =", smallst_numb)

Output:

The smallest number in the given list = 1
2)For String List

Similarly, do the same to get the smallest string in the given list.

Note: If the items in an iterable are strings, the smallest (alphabetically ordered) item is returned.

Approach:

  • Give the list (string list) as static input and store it in a variable.
  • Pass the given list as an argument to the min() function to get the smallest string in the given list. If the items in an iterable are strings, the smallest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the smallest string in the given list (alphabetically ordered).
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_strlst = ["hello", "this", "is", "btechgeeks"]
# Pass the given list as an argument to the min() function to get the smallest
# string in the given list.If the items in an iterable are strings,
# the smallest (alphabetically ordered) item is returned.
# Store it in another variable.
smallst_str = min(gvn_strlst)
# Print the smallest string in the given list (alphabetically ordered).
print("The smallest string in the given list (alphabetically ordered) is :")
print(smallst_str)

Output:

The smallest string in the given list (alphabetically ordered) is :
btechgeeks
3)Passing multiple arguments

To get the smallest number from the given numbers.

# Give some random numbers as arguments to the min() function.
smallst_numb = min(5, 10, -1, 9, 6)
# Print the smallest number from the given numbers
print("The smallest number from the given numbers = ", smallst_numb)

Output:

The smallest number from the given numbers =  -1

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

1)For Number List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the min() function to get the smallest element in the given list.
  • Store it in another variable.
  • Print the smallest element(number) in the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the min() function to get the smallest
# element in the given list.
# Store it in another variable.
smallst_numb = min(gvn_lst)
# Print the smallest element(number) in the given list.
print("The smallest number in the given list =", smallst_numb)

Output:

Enter some random List Elements separated by spaces = 6 2 1 -2 0
The smallest number in the given list = -2
2)For String List

Approach:

  • Give the list (string list) as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the min() function to get the smallest string in the given list. If the items in an iterable are strings, the smallest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the smallest string in the given list (alphabetically ordered).
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_strlst = list(input(
   'Enter some random List Elements separated by spaces = ').split())
# Pass the given list as an argument to the min() function to get the smallest
# string in the given list.If the items in an iterable are strings,
# the smallest (alphabetically ordered) item is returned.
# Store it in another variable.
smallst_str = min(gvn_strlst)
# Print the smallest string in the given list (alphabetically ordered).
print("The smallest string in the given list (alphabetically ordered) is :")
print(smallst_str)

Output:

Enter some random List Elements separated by spaces = good morning all
The smallest string in the given list (alphabetically ordered) is :
all

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.