📖 Phase 1: Programming Fundamentals
📌 Topic 9: Conditional Statements (if, elif, else)
Conditional statements allow a program to make decisions based on whether a condition is "True" or "False".
Think about a real-life decision:
👉 If it is raining → Take an umbrella. ☔
👉 Otherwise → Don't take an umbrella.
Programming works in a similar way.
Why Do We Need Conditional Statements?
They allow programs to:
• Make decisions
• Execute different blocks of code
• Validate user input
• Control program behavior
• Handle different scenarios
1. The "if" Statement
The "if" statement executes a block of code only when a condition is "True".
Example:
age = 25
if age >= 18:
print("You are eligible to vote.")
Output: You are eligible to vote.
If the condition is "False", the code inside the "if" block will not execute.
Important: Indentation
Python uses indentation to define blocks of code.
Correct:
age = 25
if age >= 18:
print("Eligible")
Incorrect:
age = 25
if age >= 18:
print("Eligible")
The second example will produce an indentation error.
2. The "else" Statement
"else" executes when the "if" condition is "False".
Example:
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Output: Not eligible to vote
Think of it as: If condition is true → Do this. Otherwise → Do that.
3. The "elif" Statement
"elif" means "else if". It allows you to check multiple conditions.
Example:
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Output: Grade A
Python checks the conditions from top to bottom and executes the first condition that is "True".
4. Multiple Conditions
You can combine conditions using logical operators.
Example:
age = 25
has_id = True
if age >= 18 and has_id:
print("Access granted")
else:
print("Access denied")
Output: Access granted
5. Nested "if" Statements
An "if" statement can be placed inside another "if" statement.
Example:
age = 25
country = "India"
if age >= 18:
if country == "India":
print("Eligible")
Nested conditions are useful when one decision depends on another.
6. Short-Hand "if"
For simple conditions, Python allows a one-line "if".
age = 25
if age >= 18: print("Adult")
Output: Adult
Common Beginner Mistakes
❌ Forgetting the colon ":" after "if", "elif", and "else"
❌ Using incorrect indentation
❌ Using "=" instead of "==" for comparison
❌ Writing conditions in the wrong order
Example of wrong order:
marks = 95
if marks >= 60:
print("Grade B")
elif marks >= 90:
print("Grade A+")
Output: Grade B
Why? Because Python stops at the first condition that is true.
Correct order:
if marks >= 90:
print("Grade A+")
elif marks >= 60:
print("Grade B")
Real-World AI Example
Conditional statements are also used in AI applications.
confidence = 0.92
if confidence >= 0.90:
print("High confidence prediction")
elif confidence >= 0.70:
print("Medium confidence prediction")
else:
print("Low confidence prediction")
Output: High confidence prediction
This type of logic can be used alongside Machine Learning models to decide what action to take based on a prediction or confidence score.
Key Takeaways
• "if" checks a condition
• "elif" checks additional conditions
• "else" handles everything that doesn't match the previous conditions
• Python uses indentation to define code blocks
• Conditions can be combined using "and", "or", and "not"
• Python executes the first matching condition in an "if/elif/else" chain
➡️ Double Tap ❤️ For More