Python min() – Find the Smallest Value in Python

How to Use the min() Function in Python

In this tutorial, you will learn how to find the smallest value in Python using the min() function. This tutorial also provides an example of finding the smallest value in a list without using the built-in functions min() and sort().

Python min() Function Syntax

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

min(list)

Or:

min(arg1, arg2, *args)

Examples:

  1. min([100, 200, 300, 400])
  2. min(10, 20, 40, 30)

Remark

  • List: the list containing the values for which you want to find the minimum value.

Examples

Using min() to find the smallest value in a group of numbers

The following code shows an example of finding the smallest of three numbers:

  1. # find the smallest of 3 numbers
  2. min_value = min(200, 170, 125)
  3. print("The smallest value: ", min_value)

Output:

The smallest value:  125

The following code shows an example of finding the smallest of four numbers:

  1. # find smallest of 4 numbers
  2. min_value = min(40, 80, 125, 90)
  3. print("The smallest value: ", min_value)

Output:

The smallest value:  40

Finding the smallest value in the list

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

  1. # find the smallest value in a list
  2. numbers = [110, 220, 300, 90, 230, 140, 440, 130, 170]
  3. smallest_value = min(numbers)
  4. print("The smallest value: ", smallest_value)

Output:

The smallest value:  90

Finding the smallest number without min()

The following program uses a FOR loop and IF statement to find the smallest number in a list:

  1. # find the smallest value in a list
  2. numbers = [1200, 1280, 1100, 920, 320, 1240]
  3. min = numbers[0]
  4. for number in numbers:
  5.     if number < min:
  6.         min = number
  7. print("The smallest number: ", min)

Output:

The smallest number:  320

The following program uses a FOR loop and an IF statement to find the second-smallest number in the list:

  1. # find the second smallest number in a list
  2. numbers = [1200, 1280, 1100, 920, 320, 1240]
  3. length = len(numbers)
  4. for i in range(length):
  5.     for j in range(i + 1, length):
  6.         if numbers[i] > numbers[j]:
  7.             num1 = numbers[i]
  8.             num2 = numbers[j]
  9.             numbers[i] = num2
  10.             numbers[j] = num1
  11. second_smallest = numbers[1]
  12. print("The second smallest number: ", second_smallest)

Output:

The second smallest number:  920

The following program uses a FOR loop and an IF statement to find the third smallest number in the list:

  1. # find the third smallest number in a list
  2. numbers = [1200, 1280, 1100, 920, 320, 1240]
  3. length = len(numbers)
  4. for i in range(length):
  5.     for j in range(i + 1, length):
  6.         if numbers[i] > numbers[j]:
  7.             num1 = numbers[i]
  8.             num2 = numbers[j]
  9.             numbers[i] = num2
  10.             numbers[j] = num1
  11. third_smallest = numbers[2]        
  12. print("The third smallest number: ", third_smallest)

Output:

The third smallest number:  1100

Read more:

Summary

In this tutorial, you learned how to find the smallest value in a list of numbers with/without 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