0
0
PythonProgramBeginner · 2 min read

Python Program to Find Largest of Three Numbers

You can find the largest of three numbers in Python using max(num1, num2, num3) or by comparing them with if statements like if num1 >= num2 and num1 >= num3: largest = num1.
📋

Examples

Inputnum1=5, num2=10, num3=3
Output10
Inputnum1=7, num2=7, num3=2
Output7
Inputnum1=-1, num2=-5, num3=-3
Output-1
🧠

How to Think About It

To find the largest number among three, compare each number with the others using if conditions. The number that is greater than or equal to the other two is the largest.
📐

Algorithm

1
Get the three numbers as input.
2
Compare the first number with the second and third.
3
If the first number is greater or equal to both, it is the largest.
4
Otherwise, compare the second number with the third.
5
If the second number is greater or equal, it is the largest.
6
Otherwise, the third number is the largest.
7
Return or print the largest number.
💻

Code

python
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))

if num1 >= num2 and num1 >= num3:
    largest = num1
elif num2 >= num1 and num2 >= num3:
    largest = num2
else:
    largest = num3

print('The largest number is', largest)
Output
Enter first number: 5 Enter second number: 10 Enter third number: 3 The largest number is 10.0
🔍

Dry Run

Let's trace the example where num1=5, num2=10, num3=3 through the code

1

Input numbers

num1=5, num2=10, num3=3

2

Check if num1 is largest

Is 5 >= 10 and 5 >= 3? No

3

Check if num2 is largest

Is 10 >= 5 and 10 >= 3? Yes

4

Assign largest

largest = 10

5

Print result

The largest number is 10

StepConditionResultLargest So Far
Check num15 >= 10 and 5 >= 3FalseNone
Check num210 >= 5 and 10 >= 3True10
Assign largest--10
💡

Why This Works

Step 1: Compare numbers

The program uses if and elif to compare the numbers two at a time.

Step 2: Find largest

The number that passes the comparison is stored as the largest.

Step 3: Output result

Finally, the program prints the largest number found.

🔄

Alternative Approaches

Using max() function
python
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
largest = max(num1, num2, num3)
print('The largest number is', largest)
This method is shorter and uses Python's built-in function for simplicity.
Using nested if-else
python
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
if num1 >= num2:
    if num1 >= num3:
        largest = num1
    else:
        largest = num3
else:
    if num2 >= num3:
        largest = num2
    else:
        largest = num3
print('The largest number is', largest)
This approach uses nested conditions to find the largest number step-by-step.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and the largest number, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using max() is simplest and most readable.

ApproachTimeSpaceBest For
If-elif comparisonsO(1)O(1)Clear logic and learning comparisons
max() functionO(1)O(1)Concise and readable code
Nested if-elseO(1)O(1)Stepwise decision making
💡
Use Python's built-in max() function for a clean and simple solution.
⚠️
Beginners often forget to use and when comparing two conditions together, causing wrong results.