max() function in PythonIn this tutorial, we will discuss the max() function and how to use it in the Python programming language. We will also consider various examples for better understanding. So, let’s get started. Understanding the Python max() functionThe max() function in Python returns the most significant data element in an iterable. We can also use this function in order to find the largest data element between more than one parameter. The Python max() function has two different forms:
The max() function with iterable argumentsWe can use the following syntax to find the largest data element in an iterable: Syntax: Parameters:
Let us consider an example based on this method in order to find the largest element in a list. Example: 1 Output: The largest number in the list: 15 Explanation: In the above snippet of code, we have created a list as my_digits. We have then used the max() function on the list in order to obtain the largest element in the list. At last, we have printed the resultant element for the users. If the data elements in an iterable are strings, the max() function will return the largest element (ordered alphabetically). Let us consider an example based on the largest string in a list. Example: 2 Output: The largest string in the list: Papaya Explanation: In the above snippet of code, we have defined a list as my_strings consisting of various strings. We have then used the max() function on the list to find the largest element as a string. At last, we have printed the element for the users. Generally, the max() function returns the largest key in the case of dictionaries. We can also use the key parameter in order to find the key of the dictionary having the largest value. Let us consider an example demonstrating the use of the max() function in dictionaries. Example: 3 Output: The largest key in the dictionary: 4 The Key of the largest value in the dictionary: -5 The largest value in the dictionary: 25 Explanation: In the above snippet of code, we have created a dictionary with some values respected to their keys. We have then used the max() function in order to find the largest key within the dictionary. We have then used the max() function to find the key with the largest value and print that value for the users. Moreover, we can observe that in the second statement of the max() function in the above example, we have passed a lambda function to the key parameter. This function will return the values of dictionaries. Based on the returned values, the max() function will return the key having the largest value. Points to Remember: If a void iterator is passed to the function, a ValueError exception is raised. In order to avoid this exception, we can include the default parameter in the function. In the case of multiple iterators, the largest element from the specified iterators will be returned. The max() function with object argumentsWe can use the following syntax in order to find the largest object between more than one parameter. Syntax: Parameters:
Thus, the max() function helps us in finding the largest element between multiple objects. Let us consider an example in order to find the maximum number among the given numbers. Example: Output: The largest number: 13 Explanation: In the above snippet of code, we have used the max() function to find the largest element among the specified objects as the parameters and printed the result for the users. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263474.html