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
# Python code to demonstrate the working of
# max()
# printing the maximum of 4,12,43.3,19,100
print("Maximum of 4,12,43.3,19 and 100 is : ",end="")
print (max( 4,12,43.3,19,100 ) )
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.
Python3
# Python code to demonstrate the working of
# min()
# printing the minimum of 4,12,43.3,19,100
print("Minimum of 4,12,43.3,19 and 100 is : ",end="")
print (min( 4,12,43.3,19,100 ) )
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
# Python code to demonstrate the Exception of
# min() and max()
# printing the minimum of 4,12,43.3,19, "GeeksforGeeks"
# Throws Exception
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
# Python code to demonstrate the Application of
# min() and max()
# printing the word occurring 1st among these in dict.
# "geeks", "manjeet", "algorithm", "programming"
print("The word occurring 1st in dict. among given is : ",end="")
print (min( "geeks", "manjeet", "algorithm", "programming" ) )
# printing the word occurring last among these in dict.
# "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
sys.maxint in Python
In Python 2, sys.maxint refers to the maximum value an integer can hold on a given platform. This value is:2³¹ - 1 = 2147483647 on a 32-bit system2â¶Â³ - 1 = 9223372036854775807 on a 64-bit systemsys.maxint was typically used as a very large number in algorithms requiring an upper bound, like finding
3 min read
Python min() Function
Python min() function returns the smallest value from a set of values or the smallest item in an iterable passed as its parameter. It's useful when you need to quickly determine the minimum value from a group of numbers or objects. For example:Pythona = [23,25,65,21,98] print(min(a)) b = ["banana",
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
What does // mean in Python ?
In Python, the // operator is known as the floor division operator. It is used to perform division between two numbers while rounding down the result to the nearest whole number. This behaviour is different from the regular division operator /, which returns a floating-point result, even when both o
3 min read
Python List max() Method
max() function in Python is a built-in function that finds and returns the largest element from the list. Let's understand it better with an example:Pythona = [2,3,6,1,8,4,9,0] print(max(a))Output9 Syntaxmax(listname)Parameter:listname : Name of list in which we have to find the maximum value.Return
4 min read