{"id":24823,"date":"2021-11-02T09:54:18","date_gmt":"2021-11-02T04:24:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=24823"},"modified":"2021-11-05T20:37:52","modified_gmt":"2021-11-05T15:07:52","slug":"python-max-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-max-function-with-examples\/","title":{"rendered":"Python max() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python len() Function with Examples<\/a> The max() function returns the item with the highest value or the highest value in an iterable.<\/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 max() returns the iterable’s largest 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 largest string in the given list.<\/p>\n Note:<\/strong> If the items in an iterable are strings, the largest (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 greatest 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 In the previous article, we have discussed Python len() Function with Examples max() Function in Python: The max() function returns the item with the highest value or the highest value in an iterable. If the values are strings, they are compared alphabetically. Syntax: max(iterable) or max(n1, n2, n3,……..) Parameters iterable: An iterable that compares one …<\/p>\n
\nmax() Function in Python:<\/strong><\/p>\nmax(iterable)<\/pre>\n
max(n1, n2, n3,........)<\/pre>\n
Given List = [1, 2, 5, 6, 15, 7]<\/pre>\n
The greatest number in the given list = 15<\/pre>\n
Given List = [\"hello\", \"this\", \"is\", \"btechgeeks\"]<\/pre>\n
The largest string in the given list (alphabetically ordered) is :\r\nthis<\/pre>\n
If the items in an iterable are strings, the largest (alphabetically ordered) \r\nitem is returned.<\/pre>\n
max() 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 max() function to get the greatest\r\n# or the largest element in the given list.\r\n# Store it in another variable.\r\ngreatst_numb = max(gvn_lst)\r\n# Print the greatest element(number) in the given list.\r\nprint(\"The greatest number in the given list =\", greatst_numb)\r\n<\/pre>\n
The greatest number in the given list = 15<\/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 max() function to get the largest\r\n# string in the given list.If the items in an iterable are strings,\r\n# the largest (alphabetically ordered) item is returned.\r\n# Store it in another variable.\r\nlargst_str = max(gvn_strlst)\r\n# Print the largest string in the given list (alphabetically ordered).\r\nprint(\"The largest string in the given list (alphabetically ordered) is :\")\r\nprint(largst_str)\r\n<\/pre>\n
The largest string in the given list (alphabetically ordered) is :\r\nthis<\/pre>\n
3)Passing multiple arguments<\/h5>\n
# Give some random numbers as arguments to the max() function.\r\nlargst_numb = max(5, 10, -2, 9, 6)\r\n# Print the greatest number from the given numbers\r\nprint(\"The greatest number from the given numbers = \", largst_numb)\r\n<\/pre>\n
The greatest number from the given numbers = 10<\/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 max() function to get the greatest\r\n# or the largest element in the given list.\r\n# Store it in another variable.\r\ngreatst_numb = max(gvn_lst)\r\n# Print the greatest element(number) in the given list.\r\nprint(\"The greatest number in the given list =\", greatst_numb)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 34 50 68 12 100\r\nThe greatest number in the given list = 100<\/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 max() function to get the largest\r\n# string in the given list.If the items in an iterable are strings,\r\n# the largest (alphabetically ordered) item is returned.\r\n# Store it in another variable.\r\nlargst_str = max(gvn_strlst)\r\n# Print the largest string in the given list (alphabetically ordered).\r\nprint(\"The largest string in the given list (alphabetically ordered) is :\")\r\nprint(largst_str)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = good morning btechgeeks\r\nThe largest string in the given list (alphabetically ordered) is :\r\nmorning<\/pre>\n
\n