Skip to content
Devtoolry

Basics · beginner

Python Program to Find the Largest Among Three Numbers

Find the largest of three numbers in Python with if/elif/else and the and operator, then do it in one line with the built-in max() function.

Comparing three values is a classic exercise for if/elif/else chains. In real code you would call max(), but writing the comparisons yourself shows how conditions are checked from top to bottom and how ties are handled.

The program

Python
a, b, c = 14, 27, 9

if a >= b and a >= c:
    largest = a
elif b >= a and b >= c:
    largest = b
else:
    largest = c

print("The largest number is", largest)
Output
The largest number is 27

How it works

  1. The first condition checks whether a is at least as big as both others. 14 >= 27 is False, so the whole and expression is False.
  2. The elif checks b the same way: 27 >= 14 and 27 >= 9 are both True, so largest becomes 27.
  3. Once one branch runs, Python skips the rest of the chain, so the else is never reached.
  4. Using >= instead of > means ties still produce an answer: with 5, 5, 3 the first branch picks 5.

Watch it run line by line

The visualizer shows which condition lines run: the if line, then the elif line, then the assignment to largest. Change the numbers so c is largest and watch it fall through to else.

“Visualize step by step” opens the program in the Python Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.

Another way: With max()

max() returns the largest of any number of arguments, or of an iterable such as a list. Use this in real code.

Python
print(max(14, 27, 9))
print(max([3.5, -2, 8.25]))
Output
27
8.25

Common mistakes

  • Using > instead of >= makes the program fall through to else when two numbers tie for largest, which can print the wrong one.
  • Writing if a > b and c (instead of a > b and a > c): "and c" only checks that c is not zero.
  • Comparing strings from input() without int(): "9" > "27" is True, because strings compare character by character.

Try it yourself

  • Also print the smallest number without using min().
  • Read the three numbers with input() and convert them with int().
  • Sort the three numbers in ascending order using only if statements.

Open the program in the Python Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.

Browse all 25 Python examples, or write your own program in the online Python Compiler.