{"id":6230,"date":"2023-10-30T11:44:22","date_gmt":"2023-10-30T06:14:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=6230"},"modified":"2023-11-10T12:08:04","modified_gmt":"2023-11-10T06:38:04","slug":"python-min-function-tutorial-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-min-function-tutorial-with-examples\/","title":{"rendered":"Python : min() Function Tutorial with Examples"},"content":{"rendered":"
We’ll go through the details of Python’s min() function with examples in this article.<\/p>\n
The min() function in Python is used to find the smallest element in a collection of elements.<\/p>\n
The min() function returns the lowest-valued object, or the lowest-valued item in an iterable.<\/p>\n
If the values are strings, they are compared alphabetically.<\/p>\n
Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Return:<\/strong><\/p>\n Exceptions:<\/strong><\/p>\n Consider the following scenario:<\/strong> we have a list of numbers.<\/p>\n Since list is an Iterable, we may directly transfer it to the min() function to find the list’s minimum value.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Assume we have a string. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The min() function compared the ASCII values of the characters in the string and returned the character with the smallest ASCII value.<\/p>\n Assume we have a list of strings. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can get min value in dictionary by using lambda function as given below.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We may also transfer individual elements to the min function rather than any Iterable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n We’ll go through the details of Python’s min() function with examples in this article. The min() function in Python is used to find the smallest element in a collection of elements. Python min() function min() function Finding min value in list Finding min ascii character in string using min() function Finding min string from list …<\/p>\nmin(iterable, *[, key, default])\r\nmin(arg1, arg2, *args[, key])<\/pre>\n
Iterable : An Iterable object is one that can be traversed, such as a list or a tuple.\r\narg1,arg2 ...... :\u00a0 Multiple elements are essential.\r\nkey : A function that is applied to and object in the Iterable and returns a value based on the argument passed in.<\/pre>\n
It returns the Iterable or assigned elements element with the lowest value. If the main function is not available,\r\n the minimum value is determined by comparing the given objects. If a key function is given, instead of explicitly\r\n comparing objects, it will first call the key function on each one before comparing it to others.<\/pre>\n
When two forms are compared, TypeError is returned.<\/pre>\n
2)Finding min value in list<\/h3>\n
# given list\r\ngivenlist = [98, 234, 21, 45, 55, 12, 988]\r\n# finding min value in list using min function\r\nminvalue = min(givenlist)\r\n# print the min value\r\nprint(minvalue)\r\n<\/pre>\n
12<\/pre>\n
3)Finding min ascii character in string using min() function<\/h3>\n
\nSince String is an Iterable, we can use the min() function to find the character with the lowest ASCII value in the string.<\/p>\n# given string\r\nstring = \"onlinebtechgeeks\"\r\n# finding min ascii value character in given string\r\nminchar = min(string)\r\n# print the min ascii character\r\nprint(minchar)\r\n<\/pre>\n
b<\/pre>\n
4)Finding min string from list of strings using min() function<\/h3>\n
\nSince list is an Iterable, we may directly transfer it to the min() function to find the minimum string based on alphabetical order in the list.<\/p>\n# given list of strings\r\nstrings_list = [\"hello\", \"this\", \"is\", \"BTechGeeks\"]\r\n# finding min string in given list of strings\r\nminstring = min(strings_list)\r\n# print the min string\r\nprint(minstring)\r\n<\/pre>\n
BTechGeeks<\/pre>\n
5)Finding min value and key in dictionary<\/h3>\n
# given dictionary\r\ndictionary = {'Hello': 238, 'This': 135,\r\n 'is': 343, 'BTechGeeks': 50, 'Platform': 688}\r\n# fet the min value and key in dictionary using lambda and min () function\r\nmin_Value = min(dictionary.items(), key=lambda x: x[1])\r\nprint('Min value in dictionary : ', min_Value)\r\n<\/pre>\n
Min value in dictionary : ('BTechGeeks', 50)<\/pre>\n
6)min() function with multiple arguments<\/h3>\n
# min() function with multiple arguments\r\nminvalue = min(100, 24, 454, 22, 989, 12, 5, 467)\r\n# print the min value\r\nprint(minvalue)\r\n<\/pre>\n
5<\/pre>\n
\n