Home / Python / Example programs / Conditions

Python program: Largest of three numbers

Find the largest of three numbers, with if/elif and with max().

The code

a, b, c = 14, 27, 9

if a >= b and a >= c:
    largest = a
elif b >= c:
    largest = b
else:
    largest = c
print("with if:", largest)

print("with max:", max(a, b, c))

Output

with if: 27
with max: 27

How it works

The if/elif version shows the logic; in real code, max does it in one step and works for any number of values.

Python features used

Learn the ideas behind it in the free lesson Making Decisions.