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


How to Use the max() Function in Python

In this tutorial, you'll learn how to find the largest value in Python using the max() function. Also, this tutorial illustrates examples of how to find the largest value in a list without using the built-in function max().

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: The list containing values that you want find the largest value

Examples

Find the Maximum Value in a Group of Numbers Using max()

The following code illustrates an example of finding the largest of 3 numbers:

# find the largest of 3 numbers
max_value = max(207025)
print("The largest value: ", max_value)

Output:

The largest value: 70

The following code illustrates an example of finding the largest of 4 numbers:

# find largest of 4 numbers
max_value = max(20702590)
print("The largest value: ", max_value)

Output:

The largest value: 90

The following code illustrates an example of finding the largest of 5 numbers:

# program to find largest of 5 numbers
max_value = max(207012590100)
print("The largest value: ", max_value)

Output:

The largest value: 125

Find the Largest Value in a List

The following program finds the largest value in a list of numbers:

# find the maximum value in a list
numbers = [1020100902040403070]
max_value = max(numbers)
print("The maximum value: ", max_value)

Output:

The maximum value: 100

Find the Largest Number in a List without Using max()

The following program finds the largest number in a list using the FOR loop and IF statements:

# find the largest value in a list
numbers = [200.50180.30100.1090.7020.20140.40140.8030.9370.20]
max = 0
for number in numbers:
    if number > max:
        max = number
print("The largest number: ", max)

Output:

The largest number: 200.5

The following program finds the 2nd largest number in a list using the FOR loop and IF statements:

# find the second largest number in a list
numbers = [200.50180.30100.1090.7020.20140.40140.8030.9370.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] = num1
second_largest = numbers[length-2]
print("The second largest number: ", second_largest)

Output:

The second largest number: 180.3

The following program finds the 3rd largest number in a list using the FOR loop and IF statements:

# find the third largest number in a list
numbers = [200.50180.30100.1090.7020.20140.40140.8030.9370.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] = num1
third_largest = numbers[length-3]
print("The third largest number: ", third_largest)

Output:

The third largest number: 140.8

Check out the following links to learn more about the FOR loop and IF statements:

In this tutorial, you've learned how to find the largest value in a list of numbers with/without using the built-in function, max().

Leave a Reply

Your email address will not be published. Required fields are marked *