Python min() Function



The Python min() function is used to retrieve the smallest element from the specified iterable.

Finding the minimum of two given numbers is one of the usual calculations we perform. In general min is an operation where we find the smallest value among the given values. For example, from the values “10, 20, 75, 93” the minimum value is 10.

Syntax

Following is the syntax of Python min() function −

min(x, y, z, ....)

Parameters

  • x, y, z − This is a numeric expression.

Return Value

This function returns smallest of its arguments.

Example

The following example shows the usage of the Python min() function. Here we are retrieving the smallest number of the arguments passed to the function.

print ("min(80, 100, 1000) : ", min(80, 100, 1000))
print ("min(-20, 100, 400) : ", min(-20, 100, 400))
print ("min(-80, -20, -10) : ", min(-80, -20, -10))
print ("min(0, 100, -400) : ", min(0, 100, -400))

When we run above program, it produces following result −

min(80, 100, 1000) :  80
min(-20, 100, 400) :  -20
min(-80, -20, -10) :  -80
min(0, 100, -400) :  -400

Example

In here, we are creating a list. Then the smallest element of the list is retrieved using min() function.

# Creating a list
List = [74,587,24,92,4,2,7,46]
res = min(List)
print("The smallest number in the list is: ", res)

While executing the above code we get the following output −

The smallest number in the list is:  2

Example

In the example below, a list of equal length of strings is created. Then the smallest string is retrieved based on the alphabetical order:

# Creating the string
Str = ['dog','cat','kit']
small = min(Str)
print("The minimum of the strings is: ", small)

Output of the above code is as follows −

The minimum of the strings is:  cat

Example

We can also use the min() function in the dictionary for finding the smallest key as shown below:

# Creating the dictionary
dict_1 = {'Animal':'Lion', 'Kingdom':'Animalia', 'Order':'Carnivora'}
small = min(dict_1)
print("The smallest key value is: ", small)

Following is an output of the above code −

The smallest key value is:  Animal
python_built_in_functions.htm
Advertisements