# max () and min () in Python

Python Methods and Functions

Maximum()

This function is used to calculate the maximum of values ​​passed in its argument and the lexicographically largest value if strings are passed as arguments.

`  Syntax:  max (a, b, c, ..)  Parameters:   a, b, c, ..:  similar type of data.  Return Value:  Returns the maximum of all the arguments.  Exceptions:  Returns TypeError when conflicting types are compared. `

` `

` # Python code to demonstrate how it works # Max ()   # print max 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 `

min ()

This function is used to calculate the minimum of values ​​passed in its argument, and the lexicographically smallest value if passed as arguments lines.

`  Syntax:  min (a, b, c, ..)  Parameters:   a, b, c, ..:  similar type of data.  Return Value:  Returns the minimum of all the arguments.  Exceptions:  Returns TypeError when conflicting types are compared. `

` `

` # Python code to demonstrate how it works # min ()   # print at least 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 )) < / td> `

` `

Output:

` Minimum of 4,12,43.3,19 and 100 is : 4 `

exception

1. TypeError: These functions throw a TypeError when comparing conflicting data types .

 ` # Python code to demonstrate the exception ` ` # min () and max () `   ` # print at least 4,12,43.3,19, "GeeksforGeeks" ` ` # Throws an exception ` ` print ` ` (` ` "Minimum of 4,12,43.3,19 and Python.Engineering is: "` `, end ` ` = ` `" ") ` ` print ` ` (` ` min ` ` (` ` 4 ` `, ` ` 12 ` `, ` ` 43.3 ` `, ` ` 19 ` `, ` ` "GeeksforGeeks" ` `)) `

Output:

` Minimum of 4,12,43.3,19 and Python.Engineering is: `

Runtime error:

` Traceback (most recent call last): File "/home/b5da1d7f834a267f94fbbefe1b31a83c.py", line 7, in print (min (4,12,43.3,19, "Python.Engineering ")) TypeError: unorderable types: str () & lt; int () `

Practical use

One of the practical uses among many is finding the lexicographically largest and smallest lines, ie The line that appears first or last in the dictionary.

 ` # Python code to demonstrate usage ` ` # min () and max () `   ` # prints the first word in the dict. ` ` # geeks, manjeet, algorithm, programming ` ` print ` ` (` ` "The word occurring 1st in dict. among given is: "` `, end ` ` = ` `" ") `` print ( min ( "geeks" , "manjeet" , "algorithm" , " programming " ))    # prints the last word in the dict. # "geeks", "manjeet", "algorithm", " ; programming & quot; 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 `

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article with contribute.python.engineering or post the article [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.