{"id":24826,"date":"2021-11-02T09:54:32","date_gmt":"2021-11-02T04:24:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=24826"},"modified":"2021-11-05T20:38:05","modified_gmt":"2021-11-05T15:08:05","slug":"python-min-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-min-function-with-examples\/","title":{"rendered":"Python min() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python max() Function with Examples<\/a> The min() function returns the item with the lowest value or the item in an iterable with the lowest value.<\/p>\n If the values are strings, they are compared alphabetically.<\/p>\n Syntax:<\/strong><\/p>\n or<\/p>\n Parameters<\/strong><\/p>\n iterable:<\/strong> An iterable that compares one or more items.<\/p>\n n1, n2, n3,……..:<\/strong> a single or multiple items to compare.<\/p>\n Return Value:<\/strong><\/p>\n min() returns the iterable’s smallest element.<\/p>\n 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 Note:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, do the same to get the smallest string in the given list.<\/p>\n Note:<\/strong> If the items in an iterable are strings, the smallest (alphabetically ordered) item is returned.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n To get the smallest number from the given numbers.<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python max() Function with Examples min() Function in Python: The min() function returns the item with the lowest value or the item in an iterable with the lowest value. If the values are strings, they are compared alphabetically. Syntax: min(iterable) or min(n1, n2, n3,……..) Parameters iterable: An iterable …<\/p>\n
\nmin() Function in Python:<\/strong><\/p>\nmin(iterable)<\/pre>\n
min(n1, n2, n3,........)<\/pre>\n
Given List = [1, 2, 5, 6, 15, 7]<\/pre>\n
The smallest number in the given list = 1<\/pre>\n
Given List = [\"hello\", \"this\", \"is\", \"btechgeeks\"]<\/pre>\n
The smallest string in the given list (alphabetically ordered) is :\r\nbtechgeeks<\/pre>\n
If the items in an iterable are strings, the smallest(alphabetically ordered) \r\nitem is returned.<\/pre>\n
min() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)For Number List<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 2, 5, 6, 15, 7]\r\n# Pass the given list as an argument to the min() function to get the smallest\r\n# element in the given list.\r\n# Store it in another variable.\r\nsmallst_numb = min(gvn_lst)\r\n# Print the smallest element(number) in the given list.\r\nprint(\"The smallest number in the given list =\", smallst_numb)\r\n<\/pre>\n
The smallest number in the given list = 1<\/pre>\n
2)For String List<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_strlst = [\"hello\", \"this\", \"is\", \"btechgeeks\"]\r\n# Pass the given list as an argument to the min() function to get the smallest\r\n# string in the given list.If the items in an iterable are strings,\r\n# the smallest (alphabetically ordered) item is returned.\r\n# Store it in another variable.\r\nsmallst_str = min(gvn_strlst)\r\n# Print the smallest string in the given list (alphabetically ordered).\r\nprint(\"The smallest string in the given list (alphabetically ordered) is :\")\r\nprint(smallst_str)\r\n<\/pre>\n
The smallest string in the given list (alphabetically ordered) is :\r\nbtechgeeks<\/pre>\n
3)Passing multiple arguments<\/h5>\n
# Give some random numbers as arguments to the min() function.\r\nsmallst_numb = min(5, 10, -1, 9, 6)\r\n# Print the smallest number from the given numbers\r\nprint(\"The smallest number from the given numbers = \", smallst_numb)\r\n<\/pre>\n
The smallest number from the given numbers = -1<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)For Number List<\/h5>\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 as an argument to the min() function to get the smallest\r\n# element in the given list.\r\n# Store it in another variable.\r\nsmallst_numb = min(gvn_lst)\r\n# Print the smallest element(number) in the given list.\r\nprint(\"The smallest number in the given list =\", smallst_numb)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 6 2 1 -2 0\r\nThe smallest number in the given list = -2<\/pre>\n
2)For String List<\/h5>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_strlst = list(input(\r\n 'Enter some random List Elements separated by spaces = ').split())\r\n# Pass the given list as an argument to the min() function to get the smallest\r\n# string in the given list.If the items in an iterable are strings,\r\n# the smallest (alphabetically ordered) item is returned.\r\n# Store it in another variable.\r\nsmallst_str = min(gvn_strlst)\r\n# Print the smallest string in the given list (alphabetically ordered).\r\nprint(\"The smallest string in the given list (alphabetically ordered) is :\")\r\nprint(smallst_str)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = good morning all\r\nThe smallest string in the given list (alphabetically ordered) is :\r\nall<\/pre>\n
\n