Largest of Three Numbers in Python
Find the largest of three numbers in Python using if-elif-else and using the built-in max() function.
- Time
O(1) - Space
O(1) - Hints3
- Read3 min
To find the largest of three numbers a, b and c in Python, check if a >= b and a >= c, then a is largest; elif b >= c, then b is largest; else c is largest. Use >= rather than > so that equal values are handled. The one-line way is max(a, b, c).
Problem statement
Write a Python program to find the largest of three numbers. Read three numbers and print the greatest one.
Write it with if-elif-else first, then with max(). Make sure it works when two or
all three numbers are equal.
Three numbers a, b and c.
The largest of the three.
Examples
a = 10, b = 25, c = 72525 is greater than both 10 and 7.
a = 5, b = 5, c = 999 is greater than both 5s.
a = 8, b = 8, c = 88All three are equal, so the largest is 8.
Constraints
- The numbers can be negative, zero or decimal
- Two or all three numbers can be equal
Think before you code
Reveal the questions to ask yourself first
- What two comparisons prove that
ais the largest? - If
ais not the largest, how many comparisons do you still need? - What should happen when
aandbare equal and bigger thanc?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Greatest of three numbers in Python with if-elif
- If
a >= b and a >= c,ais the largest. elif b >= c,bis the largest. (We already knowais not.)else,cis the largest.
Only one branch runs, from top to bottom. As soon as one condition is True, Python
skips the rest.
Find maximum of three with max()
max(a, b, c) returns the largest value. It also works for any number of values, or a
list: max([a, b, c, d]). min() does the opposite.
| Method | Lines | When to use |
|---|---|---|
if-elif-else |
6 | Exams and lab records that test conditions |
| Running maximum | 5 | When you will extend it to many numbers |
max(a, b, c) |
1 | Real code, or when built-ins are allowed |
More on conditions in if-else statements in Python.
Dry run
Walk through the example step by step
a = 10, b = 25, c = 7:
check | values | result
--------------------------+--------------------+-------
if a >= b and a >= c | 10 >= 25 is False | skip
elif b >= a and b >= c | 25 >= 10, 25 >= 7 | True
largest = b | | 25
The else branch is never reached.
Solution
Reveal the full Python solution
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print("Largest number is", largest)Output:
Enter first number: 10
Enter second number: 25
Enter third number: 7
Largest number is 25
The elif here checks b >= a again for clarity. Since we only reach it when a is
not the largest, elif b >= c alone is enough, as in the function below.
Compare the if-elif version with max()
def largest_of_three(a, b, c):
if a >= b and a >= c:
return a
elif b >= c:
return b
else:
return c
tests = [(10, 25, 7), (40, 15, 30), (5, 5, 9), (8, 8, 8), (-3, -9, -1)]
for a, b, c in tests:
print(a, b, c, "->", largest_of_three(a, b, c), max(a, b, c))
10 25 7 -> 25 25
40 15 30 -> 40 40
5 5 9 -> 9 9
8 8 8 -> 8 8
-3 -9 -1 -> -1 -1
Both methods agree for equal values and negative numbers.
Running maximum
This version is easy to extend to 4, 5 or 100 numbers.
a, b, c = 10, 25, 7
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
print(largest)
25
The same idea, run over a list, is find the largest element in an array.
Decimal numbers with max() and min()
a, b, c = 12.5, 7.25, 12.75
print(max(a, b, c))
print(min(a, b, c))
12.75
7.25
To read decimals from the user, use float(input()) instead of int(input()).
Why > instead of >= is a bug
a, b, c = 5, 5, 3
if a > b and a > c:
print("a is largest")
elif b > a and b > c:
print("b is largest")
else:
print("c is largest")
c is largest
Neither a > b nor b > a is true when they are equal, so the code falls through to
else and wrongly picks 3. Using >= fixes it.
The Java version of this page is find the largest of three numbers.
O(1)O(1)Review before the interview
Common mistakes
- Using > instead of >=. With two equal largest values, every branch fails and the code picks the wrong number.
- Writing if a > b > c only. It checks one order, and misses cases like a = 10, b = 3, c = 7.
- Forgetting int() or float() around input(). Strings compare letter by letter, so "9" > "25" is True.
- Using separate if statements instead of elif, so more than one branch can run and overwrite the answer.
Edge cases to test
- All three equal: the first branch is True and the answer is that value.
- Two equal largest values, like 5, 5, 3: >= returns 5 correctly.
- Negative numbers: -3, -9, -1 gives -1.
Interview follow-ups
- How would you find the smallest of three numbers with the same structure?
- How would you find the second largest of three numbers?
- How would you find the largest of n numbers entered by the user?
Keep practising
Find the Largest of Three Numbers
Hints · Dry run · SolutionSolve Similar problemFind the Largest Element in an Array
Hints · Dry run · SolutionSolve Similar problemFind the Second Largest Element in an Array
Hints · Dry run · SolutionSolve Similar problemEven or Odd Program in Python with Output
Hints · Dry run · SolutionSolve Related learningIf Else Statements
TutorialLearn Related learningWhat Is Python Used For
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
