Python Practice › If-Else Programs › Program 14
Largest of three numbers
Write a Python program to find the largest of three numbers.
Input
a = 45, b = 89, c = 23
Output
Largest: 89
Approach
- Compare each number with the other two using
>=.
Solution 1: if-elif
a, b, c = 45, 89, 23 if a >= b and a >= c: largest = aelif b >= a and b >= c: largest = belse: largest = cprint("Largest:", largest)Output
Largest: 89
Line by line
Line 3
a is the largest if it is at least as big as both others. >= handles equal values.
Lines 7-8
If neither a nor b is the largest, it must be c.
Solution 2: max()
print(max(45, 89, 23))Output
89
Learn the concept
New to this topic? Read the lesson, then come back.