if-elif-else in
Python
💥 Definition :
-->The if-elif-else statement is used
when we want to check multiple
conditions one by one.
1...if → first condition checked
2...elif (else if) → next condition(s)
checked if previous ones are false
3...else → executes if none of the
conditions are true
💥 General Syntax :
if condition 1:
# Code block when condition 1 is True
elif condition 2:
# Code block when condition 2 is True
elif condition 3:
# Code block when condition 3 is True
else:
# Code block when all conditions are
False
🔹 Example 1:
Checking Marks...!
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Fail")
---output:
Grade: B
✅ Explanation:
First condition (marks >= 90) → False
Second condition (marks >= 75) → True
→ so "Grade: B" is printed.
Remaining conditions are skipped.
🔹 Example 2:
Checking Number Type
num = -3
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
---Output:
Negative number
Thank you