Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Take Three Numbers and Print the Largest

Find the largest of three numbers without sorting. Students follow paired Python comparisons, tie handling with >=, and a separate final print statement.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 7 of 10
take-three-numbers-and-print-the-largest.py
1# Take three numbers as input
2num1 = float(input("Enter first number: "))
3num2 = float(input("Enter second number: "))
4num3 = float(input("Enter third number: "))
5
6# Find largest number
7if num1 >= num2 and num1 >= num3:
8 largest = num1
9elif num2 >= num1 and num2 >= num3:
10 largest = num2
11else:
12 largest = num3
13
14print(f"Largest number: {largest}")

Output

Enter first number: 10
Enter second number: 25
Enter third number: 15
Largest number: 25.0

In this one

  • Compare three candidates
  • Allow tied maxima
  • Store a result before printing

How it works

  1. 1

    Read the three numeric values.

  2. 2

    Test num1, then num2, against both competitors.

  3. 3

    Store the winning candidate and print largest.

Edge cases

What if all three are equal?

What if all three are equal?

The first branch chooses num1, which is still a largest value.

What if two share the maximum?

What if two share the maximum?

The earlier successful branch wins; the displayed maximum is unchanged.

What's going on

Each candidate must be at least as large as both other values. num1 is tested first using two comparisons connected by and.

If num1 fails, the program tests num2. Otherwise num3 must be a largest value. Using >= allows ties, so two equal maxima do not break the logic. The sample stores 25.0 in largest before printing it outside the branch chain.

What if all three are equal? The first branch chooses num1, which is still a largest value.

What if two share the maximum? The earlier successful branch wins; the displayed maximum is unchanged.

Quiz

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

Question 1

Why use and between comparisons?

Pick an answer to check it.

Question 2

What if all three are equal?

Pick an answer to check it.

Questions people ask

What if all three are equal?

The first branch chooses num1, which is still a largest value.

What if two share the maximum?

The earlier successful branch wins; the displayed maximum is unchanged.

Why use and between comparisons?

A candidate must beat or tie both others.