How to find the largest of three numbers in Python

In this Answer, we will discuss the solution for finding the largest of three numbers. This problem can be useful in many applications, such as determining the highest score in a game, selecting the largest dimension in geometry, or finding the maximum value in datasets for analysis and decision-making.

Let’s discuss the idea of finding the largest of three numbers. We can use a simple approach by comparing each number with the other two. The approach is as follows:

  • Start with three numbers, D1, D2, and D3, representing the three numbers.

  • Compare D1 with D2 and D3 to find the largest:

    • If D1 is greater than D2 and D3, then D1 is the largest.

    • If D2 is greater than D1 and D3, then D2 is the largest.

    • If neither of the above conditions is true, D3 is the largest.

  • The variable containing the largest number is the result.

Let’s illustrate the above idea visually below:

Flowchart to find the largest of three numbers
Flowchart to find the largest of three numbers

Let’s explore two programming methods to solve this problem:

  1. if-else statement

  2. Built-in max() function

The if-else statement

if-else is a conditional statement used to make decisions in our code. Let’s use this condition to compare the numbers and determine the largest one.

# Declared the three numbers
D1 = 10
D2 = 20
D3 = 30
# Check which number is the largest
if D1 > D2 and D1 > D3:
maximum = D1
elif D2 > D1 and D2 > D3:
maximum = D2
else:
maximum = D3
# Print the largest number
print("The largest number is", maximum)

Code explanation

  • Lines 2–4: D1, D2 and D3 are declared with the number 10, 20 and 30.

  • Lines 7–8: Check if D1 is largest than D2 and D3, then store D1 as the maximum.

  • Lines 9–10: If D1 is not larger, then check if D2 is larger than both D1 and D3. If so, store D2 as the maximum.

  • Lines 11–12: If both D1 and D2 are smaller, then D3 will be the largest number. Store D3 as the maximum.

  • Line 15: Print the largest number.

A built-in max() function

Pythn has a built-in function max() that can take multiple arguments and return the largest number.

# Declared the three numbers
num1 = 10
num2 = 20
num3 = 30
# Use the max function to find the largest number
maximum = max(num1, num2, num3)
# Print the largest number
print("The largest number is", maximum)
  • Lines 2–4: num1, num2 and num3 are declared with the number 10, 20 and 30.

  • Line 7: The max() function takes num1, num2, and num3 as arguments and returns the largest of the three numbers in the maximum variable.

  • Line 10: Print the largest number.

Conclusion

We have used two methods to find the largest of three numbers in Python. The first method uses an if-else statement to compare the numbers and determine the largest. The second method uses built-in max() function, which simplifies the process by directly returning the largest number among the given inputs. We can use both methods depending on the specific requirements and complexity of the problem.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved