Finding the largest of three numbers is a classic exercise for learning if-elif-else statements and comparison operators. In this lesson, you will solve it with conditions, and then with Python’s built-in max() function.
Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.
The logic
A number is the largest if it is greater than or equal to both of the others.
- If a is greater than or equal to b and c, then a is the largest.
- Otherwise, if b is greater than or equal to a and c, then b is the largest.
- Otherwise, c is the largest.
We use “greater than or equal to” (>=), so the program also works when two or all three numbers are equal.
Program 1: using if-elif-else
def largest(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
print(largest(10, 25, 7))
print(largest(5, 5, 3))
print(largest(-1, -5, -3))
print(largest(4, 4, 4))Output:
25 5 -1 4
How it works
Take largest(10, 25, 7). The first check asks whether 10 >= 25. That is False, so the program moves on. The second check asks whether 25 >= 10 and 25 >= 7. Both are True, so it returns 25.
The and operator is True only when both conditions are True.
Program 2: using the built-in max() function
Python can do this in one line:
print(max(10, 25, 7))
print(max([10, 25, 7]))
print(min(10, 25, 7))Output:
25
25
7max() takes any number of values, or a single list. min() works the same way and gives the smallest value.
Program 3: using sorted()
numbers = [10, 25, 7]
print(sorted(numbers)[-1])Output:
25sorted() puts the numbers in ascending order, and the index -1 picks the last, which is the largest. This is slower than max(), but it is useful if you also want the second largest (use [-2]).
Program 4: the largest of any number of values
The if-elif-else program works for exactly three numbers. This loop works for any list:
numbers = [4, 19, 7, 25, 11]
biggest = numbers[0]
for n in numbers:
if n > biggest:
biggest = n
print(biggest)Output:
25The program starts with the first number as the largest so far, and updates it whenever it finds a bigger one.
Which method should you use?
Method | Best for |
|---|---|
if-elif-else | Learning conditions, and exam answers |
max() | Real programs |
sorted() | Also finding the second or third largest |
Loop | Understanding how max() works inside |
Take the numbers from the user (on your computer)
a = float(input('Enter the first number: '))
b = float(input('Enter the second number: '))
c = float(input('Enter the third number: '))
print('The largest number is', largest(a, b, c))Our online compiler does not support input() yet, so use fixed values there.
Common mistakes
- Using > instead of >=. The program then fails when two numbers are equal and the largest.
- Forgetting the and operator. Writing a >= b >= c can give a wrong or confusing result.
- Naming a variable max. This hides the built-in max() function, and later calls fail.
Practice questions
- Find the smallest of three numbers using if-elif-else.
- Find the second largest of three numbers.
- Find the largest and smallest numbers in a list without using max() and min().
- Print the three numbers in descending order.
FAQs
How do you find the largest of three numbers in Python?
Use max(a, b, c), or compare the numbers with if-elif-else.
What if two numbers are equal?
The program above still works, because it uses >=. If the two equal numbers are the largest, it returns that value.
Can I find the largest of more than three numbers?
Yes. Use max() with a list, or use the loop program above.
