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.

Learn more: Python min() 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:

  1. max_value = max(20, 70, 25)
  2. print("The largest value: ", max_value)

Output:

The largest value: 70

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

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

Output:

The largest value: 90

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

  1. max_value = max(20, 70, 125, 90, 100)
  2. 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:

  1. numbers_list = [10, 20, 100, 90, 20, 40, 40, 30, 70]
  2. max_value = max(numbers_list)
  3. 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:

  1. numbers_list = [180.30, 100.10, 90.70, 20.20, 140.40, 200.50, 140.80, 30.93, 70.20]
  2. max = 0
  3. for number in numbers_list:
  4. if number > max:
  5. max = number
  6. print("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:

  1. numbers_list = [180.30, 100.10, 90.70, 20.20, 140.40, 200.50, 140.80, 30.93, 70.20]
  2. length = len(numbers_list)
  3. for i in range(length):
  4. for j in range(i + 1, length):
  5. if numbers_list[i] > numbers_list[j]:
  6. num1 = numbers_list[i]
  7. num2 = numbers_list[j]
  8. numbers_list[i] = num2
  9. numbers_list[j] = num1
  10. second_largest = numbers_list[length-2]
  11. 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:

  1. numbers = [200.50, 180.30, 100.10, 90.70, 20.20, 140.40, 140.80, 30.93, 70.20]
  2. length = len(numbers)
  3. for i in range(length):
  4. for j in range(i + 1, length):
  5. if numbers[i] > numbers[j]:
  6. num1 = numbers[i]
  7. num2 = numbers[j]
  8. numbers[i] = num2
  9. numbers[j] = num1
  10. third_largest = numbers[length-3]
  11. 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().


See also:
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

Leave a Comment