Day 2: Conditional Statements (if, else, elif)
Welcome to Day 2 of your 180-day AI and Machine Learning journey! Today, we’ll explore conditional statements in Python, which allow your programs to make decisions based on certain conditions. By the end of this article, you’ll understand how to use if, else, and elif to control the flow of your code.
What Are Conditional Statements?
Conditional statements are used to execute different blocks of code based on whether a condition is True or False. They are essential for writing dynamic and flexible programs.
The if Statement
The if statement checks a condition. If the condition is True, the code inside the if block is executed. If the condition is False, the code is skipped.
Syntax:
if condition:
# Code to execute if condition is TrueExample:
x = 10
if x > 5:
print("x is greater than 5")Here, the condition
x > 5isTrue, so the message"x is greater than 5"is printed.
The else Statement
The else statement is used to define a block of code that will be executed if the if condition is False.
Syntax:
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is FalseExample:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")Here, the condition
x > 5isFalse, so theelseblock is executed, and the message"x is not greater than 5"is printed.
The elif Statement
The elif (short for "else if") statement is used to check multiple conditions. It is placed between if and else.
Syntax:
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if all conditions are FalseExample:
x = 7
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but less than or equal to 10")
else:
print("x is 5 or less")Here, the condition
x > 10isFalse, so Python checks the next condition,x > 5. Since this isTrue, the message"x is greater than 5 but less than or equal to 10"is printed.
Nested Conditional Statements
You can also nest if statements inside other if statements to check for more complex conditions.
Example:
x = 15
if x > 10:
print("x is greater than 10")
if x > 20:
print("x is also greater than 20")
else:
print("x is not greater than 20")
else:
print("x is 10 or less")Here, the outer
ifchecks ifx > 10. IfTrue, it prints"x is greater than 10"and then checks ifx > 20.
Comparison Operators
Conditional statements often use comparison operators to evaluate conditions. Here are the most common ones:
==(equal to)!=(not equal to)>(greater than)<(less than)>=(greater than or equal to)<=(less than or equal to)
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")Logical Operators
You can combine multiple conditions using logical operators:
and: True if both conditions are True.or: True if at least one condition is True.not: Inverts the condition (True becomes False, and vice versa).
Example:
age = 20
is_student = True
if age >= 18 and is_student:
print("You are an adult student.")
else:
print("You are either not an adult or not a student.")Practice Exercise
Write a program that checks if a number is positive, negative, or zero.
Write a program that checks if a person is eligible to vote (age >= 18) and has a valid ID (
has_id = True).
Solution:
Check if a number is positive, negative, or zero:
num = -5
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")Check voting eligibility:
age = 20
has_id = True
if age >= 18 and has_id:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")Key Takeaways
Conditional statements (
if,else,elif) allow your program to make decisions.Use comparison operators (
==,>,<, etc.) to evaluate conditions.Use logical operators (
and,or,not) to combine multiple conditions.Nested
ifstatements can handle more complex logic.
What’s Next?
Tomorrow, we’ll dive into loops (for and while), which allow you to repeat a block of code multiple times. This is a powerful tool for automating repetitive tasks in AI and Machine Learning.
Congratulations on completing Day 2! You’re one step closer to mastering Python and AI. Keep practicing, and see you tomorrow! 🚀


