Table of Contents
Introduction
Welcome to the world of Python programming! As you progress in your coding journey, you’ll encounter scenarios that require handling multiple conditions and making decisions based on them. Here, we will dive deep into two powerful constructs: if-elif-else and nested if-else statements. By mastering these concepts, you’ll gain the ability to write more intelligent and flexible code.
Understanding If-Elif-Else Statements
If-elif-else statements provide a way to handle multiple conditions in a structured manner. It allows the program to evaluate different conditions sequentially and execute the code block associated with the first True condition, skipping the rest. This construct is particularly useful when you have several mutually exclusive conditions.
Syntax of If-Elif-Else Statements
The syntax of if-elif-else statements in Python is as follows:
if condition1:
# Code block executed if condition1 is True
elif condition2:
# Code block executed if condition1 is False and condition2 is True
elif condition3:
# Code block executed if condition1 and condition2 are False and condition3 is True
else:
# Code block executed if all conditions are False
Use Cases of If-Elif-Else Statements
If-elif-else statements are employed when you have multiple conditions that need to be evaluated in a specific order. This construct helps streamline decision-making by handling one condition at a time and ensuring only the appropriate code block executes.
Example Programs Using If-Elif-Else
Grade Classification
Time of Day Greeting
Exploring Nested If-Else Statements
Nested if-else statements involve having an if-else statement inside another if or else block. This allows for more complex decision-making and handling multiple levels of conditions.
Syntax of Nested If-Else Statements
The syntax of nested if-else statements in Python is as follows:
if condition1:
# Code block executed if condition1 is True
if condition2:
# Code block executed if condition1 and condition2 are True
else:
# Code block executed if condition1 is True and condition2 is False
else:
# Code block executed
Example Programs Using Nested If-Else
Ticket Pricing:
In this example, the program determines the ticket price based on the age of the person. If the age is less than 3, the ticket price is set to INR0. For ages between 3 and 12, the ticket price is INR10. For ages between 13 and 45, the ticket price is INR15. For ages above 65, the ticket price is INR12.
Weather Clothing Recommendation:
This example provides clothing recommendations based on the temperature and weather condition. If the temperature is below 10 degrees Celsius, it checks whether it’s rainy or not. If it’s rainy, it suggests wearing a raincoat and carrying an umbrella. Otherwise, it advises wearing a warm jacket. For temperatures equal to or above 10 degrees Celsius, it further checks if it’s sunny. If it’s sunny, it recommends wearing sunscreen and a hat. Otherwise, it suggests wearing a light t-shirt.
Number Range Categorization:
In this example, the program categorizes the input number into different ranges. If the number is greater than or equal to 0, it checks if it is a single-digit, two-digit, or a number with more than two digits. If the number is negative, it simply states that it is a negative number.
Conclusion
Nested if-else statements allow for more intricate decision-making by incorporating multiple levels of conditions. They help handle complex scenarios where different conditions need to be evaluated in a structured manner.
Feel free to experiment and extend these examples to incorporate additional conditions or nesting levels as per your requirements.