Conditional statements allow programs to make decisions based on conditions.
👉 Used heavily in:
✔ Data filtering
✔ Business rules
✔ Machine learning logic
🔹 1. if Statement
Used to execute code when a condition is True.
✅ Syntax
if condition:
# code
Example
age = 20
if age >= 18:
print("You can vote")
# Output: You can vote
🔹 2. if–else Statement
Used when there are two possible outcomes.
Syntax
if condition:
# code if true
else:
# code if false
Example
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")
🔹 3. if–elif–else Statement
Used when there are multiple conditions.
Syntax
if condition1:
# code
elif condition2:
# code
else:
# code
Example
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
🔹 4. Nested if Statement
An if statement inside another if.
age = 20
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")
🔹 5. Short if (Ternary Operator)
age = 20
print("Adult") if age >= 18 else print("Minor")
🎯 Today’s Goal
✔ Understand if
✔ Use if–else
✔ Use elif for multiple conditions
✔ Learn nested conditions
👉 Conditional logic is used in data filtering and decision models.
Double Tap ♥️ For More