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.
SyntaxFollowing is the syntax of Python min() function −
min(x, y, z, ....)Parameters
x, y, z − This is a numeric expression.
This function returns smallest of its arguments.
ExampleThe 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) : -400Example
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: 2Example
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: catExample
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4