w3resource

Python: min() function

min() function

The min() function is used to find the item with the smallest value in an iterable.

Version:

(Python 3.2.5)

Syntax:

min(iterable, *[, key, default])

Parameter:

Name Description Required /
Optional
iterable An iterable, with one or more items to compare. Required
key Specifies a one-argument ordering function like that used for list.sort(). Optional
default Specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised. Optional

Syntax:

min(arg1, arg2, *args[, key])

Parameter:

Name Description Required /
Optional
arg1, arg2, ..args If one positional argument is provided, iterable must be a non-empty iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned. Required
key Specifies a one-argument ordering function like that used for list.sort(). Optional

Return value:

The smallest item in an iterable.

Example: Python min() function

# using min(arg1, arg2, *args)
print('Minimum is:', min(2, 5, 3, 7, 6))

# using min(iterable)
num = [1, 3, 4, 10, 25, 15, 7]
print('Minimum is:', min(num))

Output:

Minimum is: 2
Minimum is: 1

Pictorial Presentation:

Python: Built-in-function - min() function

Pictorial Presentation:

Python: Built-in-function - min() function

Example: Python min() function

x = [25, 100, 1500, 725]
y = [10, 5]
z = [35, 525, 98]

# using min(iterable, *iterables, key)
print('Minimum is:', min(x, y, z, key=len))

Output:

Minimum is: [10, 5]

Python Code Editor:

Previous: memoryview()
Next: next()

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.