Python max() Function – Find the Largest Value in Python

How to Use the max() Function in Python

This tutorial explains how to use the Python max() function to find the maximum value. This tutorial will also show you an example of how to find the max value in a list without using the built-in max() function.

Python max() Function Syntax

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

max(list)

max(arg1, arg2, *args)

Examples:
max([1, 2, 4, 2])
max(1, 2, 4, 3)

Remark

• List: A list containing the values for which you want to find the maximum value

Examples

Using max() to find the maximum value among a group of numbers

The following code shows an example of finding the largest of three numbers:

`max_value = max(20, 70, 25)print("The largest value: ", max_value)`

Output:

The largest value: 70

The following code shows an example of finding the largest of four numbers:

`max_value = max(20, 70, 25, 90)print("The largest value: ", max_value)`

Output:

The largest value: 90

The following code shows an example of finding the largest of five numbers:

`max_value = max(20, 70, 125, 90, 100)print("The largest value: ", max_value)`

Output:

The largest value: 125

Find the largest value in a list

The following program will find the largets value in a list of numbers:

`numbers_list = [10, 20, 100, 90, 20, 40, 40, 30, 70]max_value = max(numbers_list)print("The maximum value: ", max_value)`

Output:

The maximum value: 100

Find the largest number in a list without using max():

The following program uses a FOR loop and an IF statement to find the largest number in a list:

`numbers_list = [180.30, 100.10, 90.70, 20.20, 140.40, 200.50, 140.80, 30.93, 70.20]max = 0for number in numbers_list:    if number > max:        max = numberprint("The largest number: ", max)`

Output:

The largest number: 200.5

The following program uses a FOR loop and an IF statement to find the second largest number in a list:

`numbers_list = [180.30, 100.10, 90.70, 20.20, 140.40, 200.50, 140.80, 30.93, 70.20]length = len(numbers_list)for i in range(length):    for j in range(i + 1, length):        if numbers_list[i] > numbers_list[j]:            num1 = numbers_list[i]            num2 = numbers_list[j]            numbers_list[i] = num2            numbers_list[j] = num1second_largest = numbers_list[length-2]print("The second largest number: ", second_largest)`

Output:

The second largest number: 180.3

The following program uses a FOR loop and an IF statement to find the third-largest number in a list:

`numbers = [200.50, 180.30, 100.10, 90.70, 20.20, 140.40, 140.80, 30.93, 70.20]length = len(numbers)for i in range(length):    for j in range(i + 1, length):        if numbers[i] > numbers[j]:            num1 = numbers[i]            num2 = numbers[j]            numbers[i] = num2            numbers[j] = num1third_largest = numbers[length-3]print("The third largest number: ", third_largest)`

Output:

The third largest number: 140.8

For more information on FOR loops and IF statements, check out the links below.

Summary

This tutorial explained how to get the largest value in a list of numbers with/without using the built-in function max().

Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python