Menu Close

Python max() Function

python max function

In this article, we will give you a complete package of Python max function to find the highest value from the iterable ( list, tuple, and set ). I have a large iterable object, Then this will best function to find the highest value from them.

In the previous tutorial, we have seen all about the Python len() function to find the length of the python object.

Python max() Function Introduction

Python max() function is a Python built-in function that returns the highest value from the iterable. If the value is a string, then an alphabetical comparison will be done.

Syntax

The syntax of the max() function in python is:-

max(iterable, *iterables, key, default)

max() Parameters

Python max() function supports four parameters.

  • iterable:- An iterable such as a list, tuple, string, etc.
  • *iterables:- Optional, Any number of tables can be more than one.
  • key:- Key parameter takes a function and applies each item of the iterable.
  • default:- Default value, if the given iterable is empty.

Return Value

The return value of the max function in Python is the highest value from the iterable or total numbers.

Python max() example

In this example section, I am going to explore the max() function along with multiple examples. Python max() function can be used in two ways: max() function with objects and max() function with iterable.

max() function with objects:

Example: Finding the highest value from four integer values


# Find highest value from the numbers.
result = max(12, 23, 43, 10)
print(result)

The output will be:- 43

Example: Find the highest value from four string values

# Return the name with highest value, order alphabetically.
result = max('Python', 'Java', 'PHP', 'HTML')
print(result)

The output will be:- Python

Example: Finding maximum value from float values


val1 = 20.12
val2 = 40.12
val3 = 30.34
val4 = 12.34
maximum = max(val1, val2, val3, val4)
print(maximum)

The Output will be:- 40.12

Example: Finding maximum value from 4 string values based on the length of the string


val1 = "Programming"
val2 = "Programming Funda"
val3 = "Python"
val4 = "Coding"
maximum = max(val1, val2, val3, val4, key=len)
print(maximum)

The Output will be:- Programming Funda

max() function with iterable:

In this section, I am going to use the Python max() function with iterables such as lists, tuples, dictionaries, strings, etc.

Example: Find the maximum item from a list


# Find highest value from the list iterable.
my_list = [12, 10, 23, 40, 100]
print(max(my_list))

The output will be:- 100

Example: Find the maximum item from a tuple


tple = ("Programming", "Programming Funda", "Python", "Java")
maximum = max(tple)
print(maximum)

The output will be:- Python

Example: Find the maximum item from a list of string items based on the length of the string item


tple = ("Programming", "Programming Funda", "Python", "Java")
maximum = max(tple, key=len)
print(maximum)

The Output will be:- Programming Funda

Example: Default value will be returned if the iterable is empty


tple = ()
maximum = max(tple, default="Programming Funda")
print(maximum)

The Output will be:- Programming Funda

Example: Find the maximum character from a string


string = "Programming"
maximum = max(string)
print(maximum)

The output will be: r

max() function exception

If you pass both strings, and integers into the Python max() function to find out the largest item from them, Then it will raise an error called TypeError.


tple = ("Coding", 12)
maximum = max(tple)
print(maximum)

Output

TypeError: '>' not supported between instances of 'int' and 'str'

Conclusion

So here we have learned how to find the highest value from the iterable using the Python max() function. You can use multiple values inside the max function separated by a comma to find the highest value from them instead of iterable.
If you like this article, please share and keep visiting for python built-in functions tutorials.

Python built-in functions


For more information:- Click Here

Thanks for your valuable time … ❤️❤️

Python next() Function
Python map() Function

Related Posts