max() Function in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The max() function in Python is used to return the largest or maximum element. Elements can be passed directly in the parameter, or an iterable can be passed in the parameter. If values of the different data types are passed, then the max function in python returns TypeError. Or if the default value is not passed in the parameter and the iterable is empty, then the max() function in python returns ValueError.

Syntax of max() function in Python

The syntax of the max() function in Python is as follows:

or

Parameters of max() function in Python

The different parameters that could be passed in the max() function are as follows:

  • x1, x2, x3,...: These are the items that the max function will compare.
  • iterable: This parameter is an iterable that contains one or more items to be compared. For example, tuple, set, list, etc.
  • iterables: This optional parameter indicates more than one iterable.
  • key: This is also an optional parameter when multiple iterables are passed. Comparisons among the iterables are performed based on the return value of this key function.
  • default: This optional parameter is returned if the iterable is empty.

Return values of max() function in Python

Using max() without iterable max() function returns the largest object among the parameters passed.

Using max() with iterable max() function returns the most significant element from an iterable.

Exceptions of max() function in Python

There could be two types of Exceptions while using the max() function in Python:

  • If the default value is not provided in the parameter and the iterable is empty, then the max function will return ValueError.
  • If values of different data types are passed in the parameter, then the max function will return TypeError.

Example of max() function in Python

Let's have a simple example for the max function in python where we have to find the maximum number from the given numbers. Let the numbers be: -1,2,0,10,15,-9,3. The maximum number among them is 15. Therefore, our max function should return 15.

Code:

Output:

What is max() function in Python?

As the name implies, the max() function in Python is used to return the largest or maximum element. As the elements can be passed directly in the parameters of the max function, or an iterable can be passed in the parameter. Therefore depending upon parameter max() function in python has two syntaxes, without iterable and with iterable.

Examples of max() function with Iterable Arguments

Example 1: To get the largest item in a list

We have to find the largest number on the list. Since the list is an iterable, we will pass the list directly in the parameter of the max() function.

Code:

Output:

Example 2: To return the Name with the highest value, ordered Alphabetically

max() function in python compares two strings based on the ASCII values of the first unique character of the strings.
For example consider the strings, "abc" and "cba". max() will return "cba" since the character 'c' has a higher ASCII value than 'a'. If the strings are "aaab" and "aaac", max() function will return "aaac" as the first unique characters of these string are 'b' and 'c' respectively.

Code:

Output:

Example 3: To return the item in a tuple with the highest value

We must find the item with the highest or largest value in the tuple. We will use the lambda function and pass this function as the key parameter of the max() function. Let's have a list of the tuple which will store the name and marks of the students.

Code:

Output:

Explanation:

Comparison among the tuples is made by the key function. Here the key function compares the tuple based on the second value of the tuple. Here, marks are the second value of the tuple.
result is storing the tuple with the highest value. Since the result is a tuple, the name of the student is present at result[0], and the marks of the student is present at result[1].

Example 4: Using the max() function in dictionaries

The max() function compares the elements of the dictionary based on the key of the element. Therefore max() function will return the highest key in the dictionary.

Code:

Output:

We can also use the lambda function to compare the elements of the dictionary based on their values.

Code:

Output:

Example 5: To find the Lexicographically largest Character in a String

If a string is passed to the max function, the max function will return the lexicographically maximum character. For example, consider the string "max function in Python". The max function will return the character 'y' as 'y' has the largest ASCII value among the characters of the string.

Code:

Output:

Examples of max() function without Iterable Arguments

Example 1: To find the maximum among the given numbers

Let's have a simple example to find the maximum number among the given numbers. We will pass the numbers directly to the parameter of the max() function.

Code:

Output:

Conclusion

  • The max() function in Python is used to return the largest or maximum element. Depending upon the parameter, it has two syntaxes, without iterable and with iterable.
  • In the case of without iterable, elements are directly passed in the parameter. In the case of iterable, the iterable can be directly passed in the parameter.
  • If the iterable is empty and the default value is not provided, then the max function will return ValueError.
  • If elements of different data types are passed to the max function, it will throw TypeError.

Read More: