max() function in Python

max() function in Python

In 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() function

The 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:

  1. max() function using iterable as arguments
  2. max() function using objects as arguments

The max() function with iterable arguments

We can use the following syntax to find the largest data element in an iterable:

Syntax:

Parameters:

  1. iterable: This parameter is iterable. For example, tuple, list, dictionary, set, and a lot more.
  2. *iterables (optional): This is an optional parameter indicating multiple iterables.
  3. key (optional): This is also an optional parameter. This parameter works when the iterables are passed, and the comparison is made on the basis of its return value.
  4. default (optional): This is another optional parameter providing the default value to the function if the specified iterable is void.

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 arguments

We can use the following syntax in order to find the largest object between more than one parameter.

Syntax:

Parameters:

  1. arg1: This parameter is an object. For example, numbers, strings, and a lot more.
  2. arg2: This parameter is also an object. For example, numbers, strings, and a lot more.
  3. *args (optional): This parameter is an optional parameter for more objects.
  4. key (optional): This parameter is also an optional parameter that works when each parameter is passed, and comparison is made on the basis of its return value.

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/tech/courses/263474.html

(0)
上一篇 2022年5月30日 19:15
下一篇 2022年5月30日 19:19

相关推荐

发表回复

登录后才能评论