IF-ELSE Conditional Statement in Python
If condition: Do this (print function) Else: Do this (print function)
Okay, here we have a simple structure of an if-else condition.
First, we make a condition, like comparing some numbers to see which one is greater, less, equal, true, or false. After that, we associate it with if, like this:
if 6 > 0:
print("The number is positive")
This print statement will execute when our condition is true. But if our condition is not true, it goes to the else part, like this:
else:
print("The number is 0 or negative")
Let’s have an example:
number = float(input("Enter a random number: "))
if number % 2 == 0:
print("The number is even")
else:
print("The number is odd")
This is the simple if-else conditional statement. Every if-else condition is built following this structure.
Let’s discuss nested if-else.
A nested if-else condition is a programming construct in which an if-else statement is placed inside another if-else statement. This allows for more complex decision-making processes where multiple conditions need to be evaluated in a hierarchical manner.
Nested if-else condition structure:
If condition 1:
Statement 1
If condition 2:
Statement 2
Else (2):
Statement 3
Else (1):
Statement 4
When the first if condition is true, it prints statement 1 and goes to the second if condition. If true, it prints statement 2. If the condition is false, it prints else statement 3. If the first if condition is false, it directly goes to else 1 and prints statement 4.
height = int(input("What is your height?"))
if height > 3:
print("You can ride")
age = int(input("Enter your age: "))
if age <= 12:
print("Pay 150 taka")
else:
print("Pay 300 taka")
else:
print("You can't ride")
Another structure & example:
if condition1:
# Code to execute if condition1 is true
if condition2:
# Code to execute if condition2 is true
else:
# Code to execute if condition2 is false
else:
# Code to execute if condition1 is false
if condition3:
# Code to execute if condition3 is true
else:
# Code to execute if condition3 is false
Example:
age = 25
is_student = True
if age < 18:
print("You are a minor.")
if is_student:
print("You are a student.")
else:
print("You are not a student.")
else:
print("You are an adult.")
if is_student:
print("You are a student.")
else:
print("You are not a student.")
Explanation:
In the above example:
- The outer if-else condition checks if age is less than 18.
- If true, it then checks the is_student condition.
- If is_student is true, it prints “You are a student.”
- If is_student is false, it prints “You are not a student.”
- If age is not less than 18, it then checks the is_student condition again.
- If is_student is true, it prints “You are a student.”
- If is_student is false, it prints “You are not a student.”
Nested if-else conditions are useful in scenarios where multiple layers of decision-making are required. For example:
- Validating multiple user inputs.
- Determining actions based on a combination of different conditions.
- Implementing complex business logic.
Elif Conditional Statement:
The elif conditional statement, short for “else if,” is used in programming to specify a new condition to test if the previous conditions in an if-else statement were false. The elif statement allows for multiple conditions to be checked sequentially until one of them is true, at which point the corresponding block of code is executed.
if condition1:
# Code to execute if condition1 is true
elif condition2:
# Code to execute if condition2 is true
elif condition3:
# Code to execute if condition3 is true
else:
# Code to execute if all conditions are false
Example:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
Explanation:
- The if statement checks if the score is greater than or equal to 90. If true, it prints “Grade: A”.
- If the score is not greater than or equal to 90, the first elif condition is checked to see if the score is greater than or equal to 80. If true, it prints “Grade: B”.
- If the score is not greater than or equal to 80, the second elif condition checks if the score is greater than or equal to 70. If true, it prints “Grade: C”.
- If the score is not greater than or equal to 70, the third elif condition checks if the score is greater than or equal to 60. If true, it prints “Grade: D”.
- If none of the above conditions are met, the else block is executed, printing “Grade: F”.
Important Points:
- You can have multiple elif statements between an if statement and an else statement.
- The first true condition encountered will execute its block of code, and the rest of the conditions will not be checked.
- The else statement is optional but can be used to handle cases where none of the specified conditions are true.
Artificial Intelligence in the Aviation and Airlines Sector
AI is transforming the aviation industry, enhancing safety, operational efficiency, and customer experience. Sazit Suvo Designer & Editor Aviation Sector and…
Who Is an AI Engineer? Career, Work, Challenges, and Required
AI Engineer is one of the most in-demand careers today. An AI Engineer designs, develops, and implements artificial intelligence (AI)-based…
Top SQL Keywords and Functions: Master 80% of Data Engineering
SQL is a fundamental skill for data engineering and analytics. With the right understanding of a few key concepts, you…
Statistics and Machine Learning: Relationship and Importance
Statistics and Machine Learning Although statistics and machine learning may sound different, they are deeply related. On one hand, statistics is…
Machine Learning and Deep Learning :Step-by-Step Learning Guide.
Machine Learning (ML) and Deep Learning (DL) Step-by-Step Learning Guide and Job Preparation Are you interested in learning Machine Learning or…
Do You Need to Learn Math First to Start Data
Should You Learn Math First or Alongside ML? “Do I need to learn Math first to start Data Science or Machine…