Python Program to Find the Largest of Three Numbers
easyif / elsePython
A favourite practical question because it tests whether you can build a decision with more than two branches.
The question
Read three different integers (one per line) and print the largest of them. Use if/elif/else — do not use max().
The code
program.py
a = int(input())
b = int(input())
c = int(input())
if a > b and a > c:
largest = a
elif b > c:
largest = b
else:
largest = c
print("Largest =", largest)Input
67 19 34
Output
Largest = 67
How it works
- 1First check whether a is bigger than both b and c — the and operator needs both comparisons to be true.
- 2If a is not the largest, the answer must be b or c, so a single comparison b > c decides it.
- 3Store the winner in largest and print it once at the end.
Common mistakes
- Writing if a > b > c — it only proves a > b and b > c, and misses the case where c > b.
- Using max() when the question says to use if-else — examiners take marks for that.
Largest of three numbers using max()
The shortest way. Use it when the question does not insist on if-else — max() accepts any number of values.
program.py
a = int(input())
b = int(input())
c = int(input())
print("Largest =", max(a, b, c))Input
12 45 7
Output
Largest = 45
Largest of three numbers using nested if
First decide between a and b, then compare the winner with c. Some question papers ask for this form by name.
program.py
a = int(input())
b = int(input())
c = int(input())
if a > b:
if a > c:
largest = a
else:
largest = c
else:
if b > c:
largest = b
else:
largest = c
print("Largest =", largest)Input
7 88 23
Output
Largest = 88
Questions students ask
- How do you find the largest of three numbers in Python without max()?
- Use if-elif-else: if a is greater than both b and c, a is the largest; otherwise compare b with c. That is the program at the top of this page.
- What happens if two of the numbers are equal?
- The program still prints the right value. With 5, 5 and 3 the first condition is false, b > c is true, and it prints 5.
Now solve a similar question yourself
A new Python question every time, checked instantly.
Preparing for NIELIT O Level?
This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.
