{"id":25736,"date":"2021-12-04T09:19:57","date_gmt":"2021-12-04T03:49:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=25736"},"modified":"2021-12-04T09:19:57","modified_gmt":"2021-12-04T03:49:57","slug":"python-program-to-find-the-smallest-number-in-3-easy-methods","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-smallest-number-in-3-easy-methods\/","title":{"rendered":"Python Program to Find the Smallest Number in 3 Easy Methods"},"content":{"rendered":"
Given a list and the task is to find the smallest number in the given list.<\/p>\n
Let us see this python code in 3 easy methods. They are<\/p>\n
min() function:<\/strong><\/p>\n 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.<\/p>\n sort() Function:<\/strong><\/p>\n 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. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 1)Using the min() function in Python.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Using sort() function in Python.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) Using the for loop.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 1)Using the min() function in Python.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Using sort() function in Python.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) Using the for loop.<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 Using the min() function in Python. Using sort() function in Python. Using the for loop. min() function: min() is a Python built-in function that takes a list …<\/p>\n
\nSo, 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.<\/p>\nGiven list = [9, 6, -3, -20, 4]<\/pre>\n
The given list's [9, 6, -3, -20, 4] smallest value = -20<\/pre>\n
Given list = [3, 7, 8, 1, 0]<\/pre>\n
The given list's [3, 7, 8, 1, 0] smallest value = 0<\/pre>\n
Program to Find the Smallest Number in 3 Easy Methods<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [9, 6, -3, -20, 4]\r\n# Pass the given list to the min() function that gives the smallest value in\r\n# the given list.\r\n# Store it in another variable.\r\nsmall_numbr = min(gvn_lst)\r\n# Print the smallest value in the given list.\r\nprint(\"The given list's\", gvn_lst, \"smallest value = \", small_numbr)\r\n<\/pre>\n
The given list's [9, 6, -3, -20, 4] smallest value = -20<\/pre>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [9, 6, -3, -20, 4]\r\n# Apply sort() function to the given list that sorts the given list in\r\n# ascending order\r\ngvn_lst.sort()\r\n# Get the first element(smallest value) of the given list and store it in\r\n# a variable.\r\nsmall_numbr = gvn_lst[0]\r\n# Print the smallest value in the given list.\r\nprint(\"The given list's\", gvn_lst, \"smallest value = \", small_numbr)\r\n<\/pre>\n
The given list's [-20, -3, 4, 6, 9] smallest value = -20<\/pre>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [2, 3, 9, -10, -11, 2, 4]\r\n# Take a variable say smalest_elemnt and initialize it with the first element\r\n# of the given list\r\nsmalest_elemnt = gvn_lst[0]\r\n# Calculate the length of the given list using the len() function and store it\r\n# in a variable.\r\nlst_len = len(gvn_lst)\r\n# Loop till the length of the given list using the for loop.\r\nfor itr in range(lst_len):\r\n # Check if the iterator value of the given list is less than the above-initialized\r\n # variable value.\r\n if gvn_lst[itr] < smalest_elemnt:\r\n # If it is true, then assign the iterator value of the given list to the\r\n # above-initialized variable(smalest_elemnt).\r\n # (Now this becomes the smallest element)\r\n smalest_elemnt = gvn_lst[itr]\r\n# Print the variable smalest_elemnt which is the smallest element in the given list.\r\nprint(\"The given list's smallest element = \", smalest_elemnt)\r\n<\/pre>\n
The given list's smallest element = -11<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list to the min() function that gives the smallest value in\r\n# the given list.\r\n# Store it in another variable.\r\nsmall_numbr = min(gvn_lst)\r\n# Print the smallest value in the given list.\r\nprint(\"The given list's\", gvn_lst, \"smallest value = \", small_numbr)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 3 7 8 1 0\r\nThe given list's [3, 7, 8, 1, 0] smallest value = 0<\/pre>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Apply sort() function to the given list that sorts the given list in\r\n# ascending order\r\ngvn_lst.sort()\r\n# Get the first element(smallest value) of the given list and store it in\r\n# a variable.\r\nsmall_numbr = gvn_lst[0]\r\n# Print the smallest value in the given list.\r\nprint(\"The given list's\", gvn_lst, \"smallest value = \", small_numbr)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 5 7 8 3 9\r\nThe given list's [3, 5, 7, 8, 9] smallest value = 3<\/pre>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Take a variable say smalest_elemnt and initialize it with the first element\r\n# of the given list\r\nsmalest_elemnt = gvn_lst[0]\r\n# Calculate the length of the given list using the len() function and store it\r\n# in a variable.\r\nlst_len = len(gvn_lst)\r\n# Loop till the length of the given list using the for loop.\r\nfor itr in range(lst_len):\r\n # Check if the iterator value of the given list is less than the above-initialized\r\n # variable value.\r\n if gvn_lst[itr] < smalest_elemnt:\r\n # If it is true, then assign the iterator value of the given list to the\r\n # above-initialized variable(smalest_elemnt).\r\n # (Now this becomes the smallest element)\r\n smalest_elemnt = gvn_lst[itr]\r\n# Print the variable smalest_elemnt which is the smallest element in the given list.\r\nprint(\"The given list's smallest element = \", smalest_elemnt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = -7 5 6 10 -12 12\r\nThe given list's smallest element = -12<\/pre>\n","protected":false},"excerpt":{"rendered":"