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 comparsion 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.
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 comparsion 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.
Minimum of 4,12,43.3,19 and 100 is : 4
1. TypeError : These functions throw TypeError when conflicting data types are compared.
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()
One of the practical application among many are finding lexicographically largest and smallest of Strings i.e String appearing first in dictionary or last.
The word occurring 1st in dict. among given is : algorithm The word occurring last in dict. among given is : programming
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Min and Max value in list of tuples
- Use of min() and max() in Python
- Pandas - GroupBy One Column and Get Mean, Min, and Max values
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
- Python | Find Min/Max in heterogeneous list
- Python | Min/Max value in float string list
- Python | Max/Min value in Nth Column in Matrix
- Python | Max/Min of tuple dictionary values
- Python - Assign K to Non Max-Min elements in Tuple
- Python - Sort dictionary by max/min element in value list
- PyQtGraph – Getting Quick Min Max of Image View
- Python String | max()
- Python | Pandas Series.max()
- Python | Pandas dataframe.max()
- Python | Pandas Index.max()
- Python | Pandas TimedeltaIndex.max
- Python | Numpy matrix.max()
- SymPy | Permutation.max() in Python
- Python | Decimal max() method