max() and min() in Python
Last Updated :
17 Jun, 2021
This article brings you a very interesting and lesser-known function of Python, namely max() and min(). Now when compared to their C++ counterpart, which only allows two arguments, that too strictly being float, int or char, these functions are not only limited to 2 elements, but can hold many elements as arguments and also support strings in their arguments, hence allowing to display lexicographically smallest or largest string as well. Detailed functionality are explained below.
This function is used to compute the maximum of the values passed in its argument and lexicographically largest value if strings are passed as arguments.
Syntax :
max(a,b,c,..,key,default)
Parameters :
a,b,c,.. : similar type of data.
key : key function where the iterables are passed and comparison is performed
default : default value is passed if the given iterable is empty
Return Value :
Returns the maximum of all the arguments.
Exceptions :
Returns TypeError when conflicting types are compared.
Python3
print ( "The word occurring 1st in dict. among given is : " ,end = "")
print ( min ( "geeks" , "manjeet" , "algorithm" , "programming" ) )
print ( "The word occurring last in dict. among given is : " ,end = "")
print ( max ( "geeks" , "manjeet" , "algorithm" , "programming" ) )
|
Output :
Maximum of 4,12,43.3,19 and 100 is : 100
This function is used to compute the minimum of the values passed in its argument and lexicographically smallest value if strings are passed as arguments.
Syntax :
min(a,b,c,.., key,default)
Parameters :
a,b,c,.. : similar type of data.
key : key function where the iterables are passed and comparison is performed
default : default value is passed if the given iterable is empty
Return Value :
Returns the minimum of all the arguments.
Exceptions :
Returns TypeError when conflicting types are compared.
Output :
Minimum of 4,12,43.3,19 and 100 is : 4
Exception
1. TypeError : These functions throw TypeError when conflicting data types are compared.
Python3
print ( "Minimum of 4,12,43.3,19 and GeeksforGeeks is : " ,end = "")
print ( min ( 4 , 12 , 43.3 , 19 , "GeeksforGeeks" ) )
|
Output :
Minimum of 4,12,43.3,19 and GeeksforGeeks is :
Runtime Error :
Traceback (most recent call last):
File "/home/b5da1d7f834a267f94fbbefe1b31a83c.py", line 7, in
print (min( 4,12,43.3,19,"GeeksforGeeks" ) )
TypeError: unorderable types: str() < int()
Practical Application
One of the practical application among many are finding lexicographically largest and smallest of Strings i.e String appearing first in dictionary or last.
Python3
print ( "The word occurring 1st in dict. among given is : " ,end = "")
print ( min ( "geeks" , "manjeet" , "algorithm" , "programming" ) )
print ( "The word occurring last in dict. among given is : " ,end = "")
print ( max ( "geeks" , "manjeet" , "algorithm" , "programming" ) )
|
Output :
The word occurring 1st in dict. among given is : algorithm
The word occurring last in dict. among given is : programming
Similar Reads
Use of min() and max() in Python
Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively,
2 min read
Python nmaxmin Module
A nmaxmin module is that library of Python which helps you to find the nth maximum number and nth minimum number in a given list. It directly returns either the maximum number or minimum number with respect to the given index (n). Here the given list must not be in sorted order as it will traverse t
2 min read
np.nanmax() in Python
numpy.nanmax()function is used to returns maximum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmax(arr, axis=None, out=None, keepdims = no value) Parameters : arr : Input array. axis : Axis along which we want the max value. Otherwise
2 min read
sys.maxint in Python
In programming, maxint/INT_MAX denotes the highest value that can be represented by an integer. In some cases, while programming, we may need to assign a value that is larger than any other integer value. Normally one assigns such values manually. For example, consider a list of integers where the m
4 min read
Python min() Function
Python min() function returns the smallest of the values or the smallest item in an iterable passed as its parameter. Example: Find Python min integer from the list [GFGTABS] Python numbers = [23,25,65,21,98] print(min(numbers)) [/GFGTABS]Output 21Python min() Function Syntaxmin(a, b, c, ..., key=fu
4 min read
Python - max() function
Python max() function returns the largest item in an iterable or the largest of two or more arguments. It has two forms. max() function with objectsmax() function with iterablePython max() function With ObjectsUnlike the max() function of C/C++, the max() function in Python can take any type of obje
4 min read
numpy.maximum() in Python
numpy.maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned.
2 min read
Python | Pandas Index.min()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.min() function returns the minimum value of the Index. The function works
2 min read
Python | Pandas Index.max()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.max() function returns the maximum value of the Index. The function works
2 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read