MAX() Function in Python

Welcome to this detailed tutorial on the max function in Python. You should know how to work with the Python Max function as a Python programmer. I will explain everything in this tutorial, including its syntax and a few real examples.

The max function in Python is a built-in function that returns the largest item in an iterable, such as a list, tuple, or string. Its syntax is max(iterable, *[, key, default]), where iterable is the sequence to be evaluated, key is an optional function to customize the comparison, and default is an optional value to return if the iterable is empty. For example, max([68, 75, 80, 72, 90, 85]) returns 90, the highest value in the list.

What is the max Function in Python?

The max function in Python is a built-in function that returns the largest item in an iterable or the largest of two or more arguments. This function is particularly useful when you need to determine the maximum value from a collection of data, such as a list or tuple in Python.

Syntax of the max Function

The syntax of the max function can be used in two primary ways:

  1. Finding the maximum value in an iterable:max(iterable, *[, key, default])
  2. Finding the maximum value among two or more arguments:max(arg1, arg2, *args[, key])

Parameters:

  • iterable: A sequence like a list, tuple, or string from which the maximum value is to be found.
  • *arg1, arg2, args: Two or more arguments among which the maximum value is to be determined.
  • key (optional): A function that specifies a one-argument ordering function like lambda.
  • default (optional): A default value to return if the provided iterable is empty.

Check out Average Function in Python

Examples of Using the max Function in Python

Let me show you some practical examples to help you understand how the max function works in Python in different scenarios.

Example 1: Find the Maximum Value in a List

Consider a list of temperatures recorded in different cities in the USA:

temperatures = [68, 75, 80, 72, 90, 85]
max_temperature = max(temperatures)
print(f"The highest temperature recorded is {max_temperature}°F.")

Output:

The highest temperature recorded is 90°F.

I executed the above Python code using VS code, and you can see the exact output in the screenshot below:

max Function in Python

Example 2: Find the Maximum Value Among Multiple Arguments

Suppose we have the heights of different buildings in New York City:

height1 = 1045  # One World Trade Center
height2 = 792   # Empire State Building
height3 = 727   # Chrysler Building

max_height = max(height1, height2, height3)
print(f"The tallest building is {max_height} feet tall.")

Output:

The tallest building is 1045 feet tall.

Here is the exact output in the screenshot below:

max Function in Python examples

Read Strip Function in Python

Example 3: Using the key Parameter

Let’s find the longest name from a list of city names:

cities = ["San Francisco", "Los Angeles", "New York", "Houston", "Chicago"]
longest_city_name = max(cities, key=len)
print(f"The city with the longest name is {longest_city_name}.")

Output:

The city with the longest name is San Francisco.

Example 4: Handle Empty Iterables with the default Parameter

If the list of numbers is empty, you can use the default parameter to avoid errors:

numbers = []
max_number = max(numbers, default=0)
print(f"The maximum number is {max_number}.")

Output:

The maximum number is 0.

Here is the exact output in the screenshot below:

Python Max Function

Conclusion

In this tutorial, I explained the max function in Python, including its syntax and various use cases. Whether you’re working with lists, tuples, or multiple arguments, the max function helps for finding the largest value. Do you still have any questions? Leave a comment below.

You may also like the following tutorials:

Leave a Comment