How to Find the Maximum of Three Numbers in Python

To find the maximum of three numbers in Python, call the built-in max() with all three values: max(a, b, c) returns the largest one. Wrap it in a function if you need it more than once:

def largest_of_three(a, b, c):
    return max(a, b, c)

Every program below was run on Python 3.12.5 in Command Prompt, and the outputs shown are copied from those runs.

You’ll see max(), the classic if-elif-else program (plus a tie bug most copies have) and numbers typed by a user.

Find the maximum of three numbers with max()

max() takes any number of arguments and returns the biggest, including negatives and int/float mixes:

def largest_of_three(a, b, c):
    return max(a, b, c)

austin, denver, boston = 18450.75, 21309.40, 19876.00
print(f"Highest weekly sales: ${largest_of_three(austin, denver, boston):,.2f}")
print(largest_of_three(-4, -9, -1))
print(largest_of_three(2, 2.5, 2))      # int and float mix fine

Output:

Highest weekly sales: $21,309.40
-1
2.5
Command Prompt output of a Python function that returns the maximum of three numbers using max()
Denver’s $21,309.40 wins the sales comparison, and the function also handles negatives and an int-float mix.

For numbers already in a list, see finding the largest number in a list covers that, and the max() function in depth explains key and default.

Find the greatest of three numbers using if-elif-else

Homework often says “don’t use max()”. The version most tutorials print uses strict >, and it breaks when two numbers tie for the top:

def greatest_strict(a, b, c):           # common version with a hidden bug
    if a > b and a > c:
        return a
    elif b > a and b > c:
        return b
    else:
        return c

def greatest(a, b, c):                  # >= handles ties
    if a >= b and a >= c:
        return a
    elif b >= c:
        return b
    else:
        return c

for nums in [(12, 7, 3), (4, 15, 9), (9, 9, 5)]:
    print(nums, "strict:", greatest_strict(*nums), " fixed:", greatest(*nums))

Output:

(12, 7, 3) strict: 12  fixed: 12
(4, 15, 9) strict: 15  fixed: 15
(9, 9, 5) strict: 5  fixed: 9
Command Prompt output of two Python greatest-of-three functions, where the strict if-elif version returns 5 for 9, 9, 5
For (9, 9, 5) the strict version falls through to else and returns 5; the >= version returns 9.

With >=, the first branch wins any tie, and the elif only needs to compare b and c. If and or elif are new to you, see if-else in Python and logical operators in Python.

Can you find the largest of three numbers in one line?

Yes, with a conditional expression (Python’s ternary). A short loop or sorted() also skips max():

a, b, c = 48, 91, 77
biggest = a if a >= b and a >= c else (b if b >= c else c)
print("Ternary:", biggest)

biggest = a
for n in (b, c):                        # no max() and no elif chain
    if n > biggest:
        biggest = n
print("Loop:   ", biggest)
print("Sorted: ", sorted([a, b, c])[-1])

Output:

Ternary: 91
Loop:    91
Sorted:  91
Command Prompt output of a Python ternary expression, a for loop and sorted() each finding the max of three numbers
All three approaches pick 91 from 48, 91 and 77.

Nested ternaries get hard to read, so I keep them short. The loop is the idea behind finding the largest and smallest numbers in longer data, and sorting is covered in sorting a list without sort().

Take three numbers from user input

input() always returns a string, and strings are compared character by character. So "9" beats "10" because "9" comes after "1". Convert first:

a = input("First number: ")
b = input("Second number: ")
c = input("Third number: ")
print("As strings:", max(a, b, c))      # text comparison, character by character

try:
    nums = [float(x) for x in (a, b, c)]
    print("As numbers:", max(nums))
except ValueError:
    print("Please enter numbers only.")

Output:

First number: 9
Second number: 10
Third number: 4
As strings: 9
As numbers: 10.0
Command Prompt output of a Python program that reads three numbers with input() and gets 9 as strings but 10.0 as floats
Typed as 9, 10 and 4, the strings give the wrong answer, 9; converted with float(), the program correctly picks 10.0.

float() accepts decimals, and the except catches bad typing. See asking for user input in Python and on checks in checking if input is a number.

Want more beginner programs like this one? Try these:

Frequently asked questions

How do I find the maximum of three numbers in Python?

Call max(a, b, c). It returns the largest of the three values.

How do I find the greatest of three numbers without max()?

Use if-elif-else: if a >= b and a >= c returns a, elif b >= c returns b, else c.

Why does my greatest-of-three program print the wrong number?

If it uses strict >, a tie such as 9, 9, 5 falls through to the else branch. Use >= instead.

Why does max() say 9 is bigger than 10?

The values are strings from input(). Convert them with int() or float() before comparing.

How do I find the smallest of three numbers?

Use min(a, b, c), or flip the comparisons to <= in the if-elif-else version.

Where is max() documented?

See max() in the Python docs.