Python min()

Python min() builtin function takes an iterable and returns the minimum of the iterable.

Python min() builtin function can also accept two or more arguments and returns the minimum of the arguments provided.

In this tutorial, we will learn about the syntax of Python min() function, and learn how to use this function with the help of examples.

Syntax

The syntax of min() function with iterable as argument is

</>
Copy
min(iterable [, key, default])

The syntax of min() function with two or more arguments is

</>
Copy
min(arg1, arg2, args[, key])

where key is one-argument ordering function.

key and default are optional.

Returns

The function returns the minimum of iterable or the arguments, whichever is provided.

Examples

1. min() with Iterable

In this example, we will pass an iterable like list, to the min() function, and find the minimum of the list.

Python Program

</>
Copy
myList = [5, 8, 1, 9, 4]
result = min(myList)
print(f'Minimum of given iterable is {result}.')

Output

Minimum of given iterable is 1.

2. min() with Two or More Arguments

In this example, we will pass three integer values to the min() function, and find the smallest of these three numbers.

Python Program

</>
Copy
a = 5
b = 2
c = 9
result = min(a, b, c)
print(f'Minimum of given arguments is {result}.')

Output

Minimum of given arguments is 2.

3. min() with Key Function

In this example, we will find the smallest of three arguments based on a key function. The key function returns the absolute value for each item that we compare using min() function. In the following program, we take abs() function as key function. So, of the three arguments we provide to min() function, the argument with minimum magnitude is returned.

Python Program

</>
Copy
a = 5
b = 2
c = -9
keyFunc = abs
result = min(a, b, c, key=keyFunc)
print(f'Minimum of given arguments is {result}.')

Output

Minimum of given arguments is 2.

Python Programs using min() function

Conclusion

In this Python Tutorial, we have learnt the syntax of Python min() builtin function, and also learned how to use this function, with the help of Python example programs.