Find the Largest Among Three Numbers in Python

Comparing numbers is a common task in many programming applications and is often used in various decision-making scenarios, such as determining the maximum value from a set of data, such as grades/ test scores. Finding the highest salary or the recorded temperature.

For example: On Day 1 we record 37°C, on Day 2 we record 33°C and on Day 3 we record 39°C. With the help of a Python program, we can determine the highest recorded temperature from the data.

In today’s lesson, we are going to learn a Python program to find the largest among three Numbers using ‘if’ ‘elif’, and ‘else’ statements.

Python Program to Find the Largest Among Three Numbers

# Find the Largest Among Three Numbers in Python
# defined input
a = 7
b = 15
c = 2

# if elif conditions to check the largest of three numbers
if (a >= b) and (a >= c):
    max_num = a
elif (b >= a) and (b >= c):
    max_num = b
else:
    max_num = c

# display output to user
print("Largest number is: ", max_num)
  • Defining the input
    • We request the user to input three numbers
    • We define the numbers as a, b and c
  • The logic of the code
    • To determine the largest number, we can compare any number with the other two number
    • If any condition that states that ‘a’ is larger than ‘b’ and ‘c’, ‘a’ is the largest number
    • If ‘b’ is larger than ‘a’ as well as ‘c’, ’b’ is the largest number
    • If both the above-mentioned condition is false, ‘c’ is the largest number
  • Implementing the ‘if’, ‘elif’ and ‘else’ conditions
    • As discussed in the logic above, we are going to check if the variable ‘a’ is larger than ‘b’ and ‘c’.
    • Syntax : if (a >= b) and (a >= c): max_num = a
    • If the variable ‘b’ or ‘c’ is the larger number, the above condition gives a false outcome
    • Hence, we use the ‘elif’ condition to check whether ‘b’ is the larger number
    • Syntax : elif (b >= a) and (b >= c): max_num = b
    • If neither of the variable ‘a’ or ‘b’ is the larger variable, we know for sure that the variable ‘c’ is the largest number
    • Therefore Syntax: else: max_num = c
  • Displaying the output to the user
    • As we have determined the largest number from the inputs, we need to print and display the output to the user
    • We are going to use the print () command to display the result of the computation
    • Syntax: print(“Largest number is: “, max_num)

Output:

Largest number is:  15

In this program, we want to find the largest value among three numbers in Python. We have already defined three variables a, b, and c and assigned them values 7, 15, and 2, respectively. The code uses if-elif statements to compare these numbers and check which one is larger than the other. 

If ‘a’ is greater than or equal to both b and c, then the variable ‘max_num’ is assigned the value of a. If this is false, the program checks the second if statement to see if b is greater than or equal to both a and c. If this condition is true, then the variable ‘max_num’ is assigned the value of b, i.e. 15, which is the largest among the three given numbers.

Why do we use if and elif conditions

The method used in the above code uses if and elif conditions to compare the numbers and find the largest one. This is a simple and easy-to-understand method and can be easily modified to find the smallest or middle number as well.

An alternate method we can use is the built-in max() function which takes multiple arguments and returns the largest one. Similarly, we can create a list to store the numbers and then use the max() function to find the largest one among them.

Though the max() function may be simpler, it may not be as flexible in some cases where you need to perform additional operations or comparisons.

In this tutorial, we learned a Python program to find the largest among three Numbers using ‘if’ ‘elif’ and ‘else’ statements. We discussed the logic to determine the largest among the numbers as well as the importance of determining it.

FAQs on code to find the Largest Number

What will happen if two or all three numbers are equal?

In that case, the code will consider any of them as the largest number and output it in the result.

What will happen if I input a non-numeric value for a, b, or c?

For a non-numeric value, the program will result in an error.

Can I use negative numbers as input?

Yes, you can use negative numbers for a, b, or c, and the program will still work as expected.

What happens if I don’t use parentheses in the if conditions?

The program may result in an error or provide an incorrect output if you don’t use parentheses in the if conditions.

Can I use this program to find the smallest number among three numbers?

No, this program is specifically designed to find the largest number among three numbers. But, you can change the comparison operators to find the smallest number.

About The Author

Leave a Reply