Python programs · Basics
Python program to find the largest of three numbers
Finding the largest of three numbers practises comparisons and the word and.
Approach
- Read three whole numbers from one line.
- If the first is at least as large as both others, it wins.
- Otherwise compare the second and the third.
The program
a, b, c = map(int, input().split()) if a >= b and a >= c: largest = a elif b >= c: largest = b else: largest = c print("Largest:", largest)
Sample run
Input
12 45 7
Output
Largest: 45
How the code works
The first test asks whether a beats both others. If not, the winner must be b or c, so one more comparison decides. Using >= makes sure equal numbers are handled. Python's built-in max(a, b, c) does the same in one call, but writing the comparisons yourself shows how decisions work.
Try changing it
- Replace the whole
ifblock withmax(a, b, c). - Extend the program to print the smallest number as well.
Common mistakes
- Writing
a > b > c, which checks something different from "a is the largest". - Forgetting to convert the input to numbers, so the values are compared as text.