This article brings you a very interesting và lesser-known function of Python, namely max() and min(). Now when compared lớn 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 và also support strings in their arguments, hence allowing to lớn display lexicographically smallest or largest string as well. Detailed functionality are explained below.
Bạn đang xem: Min/max
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 & comparison is performeddefault : default value is passed if the given iterable is emptyReturn Value : Returns the maximum of all the arguments.Exceptions : Returns TypeError when conflicting types are compared.
This function is used to lớn compute the minimum of the values passed in its argument & 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 & comparison is performeddefault : default value is passed if the given iterable is emptyReturn Value : Returns the minimum of all the arguments.Exceptions : Returns TypeError when conflicting types are compared.
Output :Minimum of 4,12,43.3,19 và randy-rhoads-online.com is : Runtime Error :Traceback (most recent điện thoại tư vấn last): tệp tin "/home/b5da1d7f834a267f94fbbefe1b31a83c.py", line 7, in print (min( 4,12,43.3,19,"randy-rhoads-online.com" ) )TypeError: unorderable types: str()
One of the practical application among many are finding lexicographically largest and smallest of Strings i.e String appearing first in dictionary or last.
Xem thêm: Hóa Trị Của Một Nguyên Tố Trong Hợp Chất, Cách Xác Định Hóa Trị Của Một Nguyên Tố
Output :The word occurring 1st in dict. Among given is : algorithmThe word occurring last in dict. Among given is : programmingThis article is contributed by Manjeet Singh. If you like randy-rhoads-online.com & would like to contribute, you can also write an article using write.randy-rhoads-online.com or mail your article khổng lồ review-team
randy-rhoads-online.com. See your article appearing on the randy-rhoads-online.com main page & help other Geeks.Please write comments if you find anything incorrect, or you want to cốt truyện more information about the topic discussed above.
Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
Python DictionaryEnumerate() in PythonHow khổng lồ Install PIP on Windows ?Different ways khổng lồ create Pandas DataframePython String | replace()
Reading và Writing to lớn text files in PythonCreate a Pandas DataFrame from Listssum() function in PythonHow to drop one or multiple columns in Pandas Dataframe*args & **kwargs in Python