Python min() – Find the Smallest Value in Python

How to Use the min() Function in Python

In this tutorial, you'll learn how to find the smallest value in Python using the min() function. Also, this tutorial illustrates examples of how to find the smallest value in a list without using the built-in function min() or sort().

Python min() Function Syntax

The syntax of the min() function is as follows:

min(list)

min(arg1, arg2, *args)

Examples:
min([100, 200, 300, 400])
min(10, 20, 40, 30)

Remark

  • list: The list containing values that you want to find its smallest value

Examples

Find the Smallest Value in a Group of Numbers Using min()

The following code illustrates an example of finding the smallest of 3 numbers:

# find the smallest of 3 numbers
min_value = min(200170125)
print("The smallest value: ", min_value)

Output:

The smallest value: 125

The following code illustrates an example of finding the smallest of 4 numbers:

# find smallest of 4 numbers
min_value = min(408012590)
print("The smallest value: ", min_value)

Output:

The smallest value: 40

Find the Smallest Value in a List

The following program finds the smallest value in a list of numbers:

# find the smallest value in a list
numbers = [11022030090230140440130170]
smallest_value = min(numbers)
print("The smallest value: ", smallest_value)

Output:

The smallest value: 90

Find the Smallest Number without Using min()

The following program finds the smallest number in a list using the FOR loop and IF statements:

# find the smallest value in a list
numbers = [1200128011009203201240]
min = numbers[0]
for number in numbers:
    if number < min:
        min = number
print("The smallest number: ", min)

Output:

The smallest number: 320

The following program finds the 2nd smallest number in a list using the FOR loop and IF statements:

# find the second smallest number in a list
numbers = [1200128011009203201240]
length = len(numbers)
for i in range(length):
    for j in range(i + 1, length):
        if numbers[i] > numbers[j]:
            num1 = numbers[i]
            num2 = numbers[j]
            numbers[i] = num2
            numbers[j] = num1
second_smallest = numbers[1]
print("The second smallest number: ", second_smallest)

Output:

The second smallest number: 920

The following program finds the 3rd smallest number in a list using the FOR loop and IF statements:

# find the third smallest number in a list
numbers = [1200128011009203201240]
length = len(numbers)
for i in range(length):
    for j in range(i + 1, length):
        if numbers[i] > numbers[j]:
            num1 = numbers[i]
            num2 = numbers[j]
            numbers[i] = num2
            numbers[j] = num1
third_smallest = numbers[2]        
print("The third smallest number: ", third_smallest)

Output:

The third smallest number: 1100

Read more:

In this tutorial, you've learned how to find the smallest value in a list of numbers with/without using the built-in function, min().


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python max() Function – Find the Largest Value in Python

Leave a Comment