min() is a built-in function that returns the minimum value among the arguments supplied to the function. If an iterable is passed as the argument, the smallest item in the iterable is returned.
Python min() Syntax
min() for numbers
min (num1, num2, *args[, key])
- num1 (required) – first object for comparison
- num2 (required) – second object for comparison
- *args (optional) – any object for the comparison
- key (optional) – key function to which each argument is passed and the comparison is done based on the value returned by this function
There can be any number of objects supplied as arguments for comparison.
min() for iterables
min(iterable, *[, key, default])
- iterable – sequence or collection whose smallest item is to be returned.
- key (optional) – It is a function to which iterables are passed and the comparison is done based on the value returned by this function.
- default (optional) – default value that specifies an object to return in case of empty iterable. If a default value is not given when empty iterable is passed,
ValueErrorwill be raised.
When an iterable is passed as the argument, the smallest item of the iterable is returned. If multiple iterables are passed then, the list for which the key function return smaller value is returned.
Python min() Example
Example 1: Find minimum among the given numbers
>>> min(1,2,3,4) 1 >>> py_num = [3,5,7,9] >>> min(py_num) 3
Example 1: Find number having minimum remainder when divided by 10 using key function
def findMin(num): rem = 0 while(num): rem = num%10 return rem # using min(arg1, arg2, *args, key) print('Number with min remainder is:', min(11,48,33,17,19, key=findMin)) # using min(iterable, key) num = [12,48,33,17] print('Number with min remainder is:', min(num, key=findMin))
When executed, this will yield following output.
Number with min remainder is: 11 Number with min remainder is: 42