Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Find Largest of Three Numbers in Python

Two equal maxima can break a careless Python comparison. Follow this beginner three-number program and see why >= and and belong in the winning conditions.

BeginnerBasic Python ProgramsExample 7 of 25
find-largest-of-three-numbers.py
1# Program to find the largest of three numbers
2
3a = float(input("Enter first number: "))
4b = float(input("Enter second number: "))
5c = float(input("Enter third number: "))
6
7if a >= b and a >= c:
8 largest = a
9elif b >= a and b >= c:
10 largest = b
11else:
12 largest = c
13
14print("Largest number is:", largest)

Sample Output

Largest number is: 25.0

b satisfies both comparisons and is stored as largest, giving 25.0.

In this one

  • Combine conditions with and
  • Select one maximum among ties
  • Trace a fallback else branch

How it works

  1. 1

    Read a, b, and c as floats.

  2. 2

    Choose a if it is at least as large as each rival; otherwise try b.

  3. 3

    Assign c only when both earlier tests fail, then print largest.

Edge cases

Tied maxima

Enter 5, 5, and 1.

The first >= condition accepts a, so the result remains 5.0.

All negative

Enter -8, -3, and -9.

The maximum is -3.0, not the value with the largest magnitude.

What's going on

Two numbers needed a three-way split. Three numbers use the same idea with and. a must be at least as large as both b and c before we can call it a largest value.

The first condition uses >= so ties count. If it fails, the second condition gives b the same chance. If neither a nor b qualifies, c must be a largest value for ordinary finite inputs. The sample chooses 25.0 from 10.0, 25.0, and 7.0.

Replacing both >= tests with > breaks cases such as 5, 5, 1: neither tied maximum wins, and the final else picks 1. max(a, b, c) avoids the manual branching, but tracing this version teaches why the equality part belongs in the comparisons.

After this program

If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why use >= instead of > here?

Pick an answer to check it.

Question 2

What does and require?

Pick an answer to check it.

Questions people ask

Why use >= instead of > here?

It lets a tied maximum qualify instead of incorrectly falling through to c.

What does and require?

Both comparisons must be true for that candidate to win.

Which tied value is returned when a and b share the maximum?

a is assigned first, but the numeric result is the same maximum either way.