Introduction
In this lesson, we'll be writing a Python program to find the largest of three numbers. This task helps solidify your understanding of conditional statements, including if, elif, and else constructs. You'll also practice using comparison operators and structuring code for readability and maintenance. By the end of this lesson, you will be able to write a program that dynamically compares three user-provided numbers and determines which one is the largest.
Understanding Conditional Statements
Conditional statements allow your program to execute different blocks of code based on certain conditions. They play a crucial role in decision-making within your programs.
If, Elif, and Else Constructs
In Python, the if statement is used to test a specific condition. If the condition is true, the block of code within the if block is executed. If it is false, the program may proceed to the elif (else if) or else block, if present.
if condition:Evaluates the given condition. If true, the block of code inside theifstatement is run.elif condition:A short form for 'else if'. If the previous conditions were false, theelifcondition is evaluated.else:If none of the preceding conditions were true, the block followingelseis executed.
Using Comparison Operators
Comparison operators in Python include:
>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to==: Equal to!=: Not equal to
These operators are essential for constructing the conditions in your if, elif, and else blocks.
Implementing the Largest Number Program
Here's how you can use if, elif, and else together with comparison operators to find the largest of three numbers.
Step-by-Step Implementation
-
Initialize User Inputs
First, you need to solicit input values from the user, which they want to compare.
num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) num3 = float(input("Enter the third number: ")) -
Determine the Largest Number
Use a series of conditional statements to compare the numbers and determine the largest.
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)
Explanation
- The first
ifstatement checks ifnum1is greater than or equal to bothnum2andnum3. If true,num1is assigned tolargest. - If the first condition is false, the
elifstatement checks ifnum2is greater than or equal tonum1andnum3. If true,num2is assigned tolargest. - If neither
ifnorelifconditions hold true, theelseblock assignsnum3aslargestbecause it must be the largest.
Structuring Code for Readability and Maintenance
- Use meaningful variable names: Instead of using
a,b,c, use descriptive names likenum1,num2,num3to clarify their role in the program. - Comment your code: Brief descriptions help others and future you understand the purpose of particular lines, especially when constructing logical operations.
- Adopt consistent indentation: Python relies on indentation to define blocks of code. Following a consistent style increases readability and reduces errors.
Practical Example
Here's a simple user interaction example:
Enter the first number: 42
Enter the second number: 21
Enter the third number: 84
The largest number is: 84
This interaction showcases user inputs and the program's output, leveraging the logic discussed.
Summary / Key Takeaways
In this lesson, Lesson 5: Find Largest of Three Numbers, you have learned:
- How to use
if,elif, andelsestatements to implement conditional logic. - The application of comparison operators to evaluate conditions.
- Methods for accepting and using user inputs effectively.
- The importance of code structure for readability and maintenance in Python.
- Developing a practical, executable Python program to solve a real-world problem.
