Python Program to Find the Smallest Number in 3 Easy Methods

Given a list and the task is to find the smallest number in the given list.

Let us see this python code in 3 easy methods. They are

  1. Using the min() function in Python.
  2. Using sort() function in Python.
  3. Using the for loop.

min() function:

min() is a Python built-in function that takes a list as an argument and returns the number with the smallest value in the list.

sort() Function:

sort() is another Python built-in method that does not return the smallest number in the list. Instead, it arranges the items in ascending order.
So, by sorting the list, we can use indexing to access the first element of the list, which will be the smallest number in the list.

Examples:

Example1:

Input:

Given list = [9, 6, -3, -20, 4]

Output:

The given list's [9, 6, -3, -20, 4] smallest value =  -20

Example2:

Input:

Given list =  [3, 7, 8, 1, 0]

Output:

The given list's [3, 7, 8, 1, 0] smallest value = 0

Program to Find the Smallest Number in 3 Easy Methods

Method #1: Using Built-in Functions (Static Input)

1)Using the min() function in Python.

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list to the min() function that gives the smallest value in the given list.
  • Store it in another variable.
  • Print the smallest value in the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [9, 6, -3, -20, 4]
# Pass the given list to the min() function that gives the smallest value in
# the given list.
# Store it in another variable.
small_numbr = min(gvn_lst)
# Print the smallest value in the given list.
print("The given list's", gvn_lst, "smallest value = ", small_numbr)

Output:

The given list's [9, 6, -3, -20, 4] smallest value =  -20

2)Using sort() function in Python.

Approach:

  • Give the list as static input and store it in a variable.
  • Apply sort() function to the given list that sorts the given list in ascending order.
  • Get the first element(smallest value) of the given list and store it in another variable.
  • Print the smallest value in the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [9, 6, -3, -20, 4]
# Apply sort() function to the given list that sorts the given list in
# ascending order
gvn_lst.sort()
# Get the first element(smallest value) of the given list and store it in
# a variable.
small_numbr = gvn_lst[0]
# Print the smallest value in the given list.
print("The given list's", gvn_lst, "smallest value = ", small_numbr)

Output:

The given list's [-20, -3, 4, 6, 9] smallest value =  -20

3) Using the for loop.

Approach:

  • Give the list as static input and store it in a variable.
  • Take a variable say smalest_elemnt and initialize it with the first element of the given list.
  • Calculate the length of the given list using the len() function and store it in a variable.
  • Loop till the length of the given list using the for loop.
  • Check if the iterator value of the given list is less than the above-initialized variable value.
  • If it is true, then assign the iterator value of the given list to the above-initialized variable(smalest_elemnt).
  • Print the variable smalest_elemnt which is the smallest element in the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [2, 3, 9, -10, -11, 2, 4]
# Take a variable say smalest_elemnt and initialize it with the first element
# of the given list
smalest_elemnt = gvn_lst[0]
# Calculate the length of the given list using the len() function and store it
# in a variable.
lst_len = len(gvn_lst)
# Loop till the length of the given list using the for loop.
for itr in range(lst_len):
    # Check if the iterator value of the given list is less than the above-initialized
        # variable value.
    if gvn_lst[itr] < smalest_elemnt:
        # If it is true, then assign the iterator value of the given list to the
        # above-initialized variable(smalest_elemnt).
        # (Now this becomes the smallest element)
        smalest_elemnt = gvn_lst[itr]
# Print the variable smalest_elemnt which is the smallest element in the given list.
print("The given list's smallest element = ", smalest_elemnt)

Output:

The given list's smallest element =  -11

Method #2: Using Built-in Functions (User Input)

1)Using the min() function in Python.

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list to the min() function that gives the smallest value in the given list.
  • Store it in another variable.
  • Print the smallest value in the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list to the min() function that gives the smallest value in
# the given list.
# Store it in another variable.
small_numbr = min(gvn_lst)
# Print the smallest value in the given list.
print("The given list's", gvn_lst, "smallest value = ", small_numbr)

Output:

Enter some random List Elements separated by spaces = 3 7 8 1 0
The given list's [3, 7, 8, 1, 0] smallest value = 0

2)Using sort() function in Python.

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Apply sort() function to the given list that sorts the given list in ascending order.
  • Get the first element(smallest value) of the given list and store it in another variable.
  • Print the smallest value in the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Apply sort() function to the given list that sorts the given list in
# ascending order
gvn_lst.sort()
# Get the first element(smallest value) of the given list and store it in
# a variable.
small_numbr = gvn_lst[0]
# Print the smallest value in the given list.
print("The given list's", gvn_lst, "smallest value = ", small_numbr)

Output:

Enter some random List Elements separated by spaces = 5 7 8 3 9
The given list's [3, 5, 7, 8, 9] smallest value = 3

3) Using the for loop.

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Take a variable say smalest_elemnt and initialize it with the first element of the given list.
  • Calculate the length of the given list using the len() function and store it in a variable.
  • Loop till the length of the given list using the for loop.
  • Check if the iterator value of the given list is less than the above-initialized variable value.
  • If it is true, then assign the iterator value of the given list to the above-initialized variable(smalest_elemnt).
  • Print the variable smalest_elemnt which is the smallest element in the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Take a variable say smalest_elemnt and initialize it with the first element
# of the given list
smalest_elemnt = gvn_lst[0]
# Calculate the length of the given list using the len() function and store it
# in a variable.
lst_len = len(gvn_lst)
# Loop till the length of the given list using the for loop.
for itr in range(lst_len):
    # Check if the iterator value of the given list is less than the above-initialized
        # variable value.
    if gvn_lst[itr] < smalest_elemnt:
        # If it is true, then assign the iterator value of the given list to the
        # above-initialized variable(smalest_elemnt).
        # (Now this becomes the smallest element)
        smalest_elemnt = gvn_lst[itr]
# Print the variable smalest_elemnt which is the smallest element in the given list.
print("The given list's smallest element = ", smalest_elemnt)

Output:

Enter some random List Elements separated by spaces = -7 5 6 10 -12 12
The given list's smallest element = -12