Find Largest of Three Numbers
Compare three numbers and print the largest one (or that some/all are equal).
BeginnerTopic: Basic Python Programs
What You'll Learn
- Chaining relational operators with and
- Extending comparison logic to three values
- Using elif branches to cover multiple conditions
Python Find Largest of Three Numbers Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to find the largest of three numbers
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print("Largest number is:", largest)Output
Enter first number: 10 Enter second number: 25 Enter third number: 7 Largest number is: 25.0
Step-by-Step Breakdown
- 1Read three numbers from the user.
- 2Use if-elif-else with combined conditions to find the maximum.
- 3Store the result in a variable named largest.
- 4Print the largest value.
Understanding Find Largest of Three Numbers
We extend the comparison logic to three numbers using a combination of >= and logical operators and.
We check:
1.If
a is greater than or equal to both b and c.2.Else if
b is greater than or equal to both a and c.3.Otherwise,
c must be the largest (or tied for largest).This pattern generalizes the two-number comparison approach.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.