✅ Python Conditional Statements if, elif, else! 🐍✨ Conditional statements allow your program to make decisions based on different conditions.
age = 20
if age >= 18:
print("Eligible to vote")
1. What is an if Statement? Executes a block of code only if the condition is True.
age = 20
if age >= 18:
print("You are an adult")
✔ Output: You are an adult
💡 If the condition is False, the code inside if is skipped.
2. if-else Statement: Used when you want one action if the condition is True and another if it's False.
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
✔ Output: Not eligible to vote
3. if-elif-else Statement: Used to check multiple conditions.
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")
✔ Output: Grade B
4. Multiple Conditions with and: age = 25
if age >= 18 and age <= 60:
print("Working age")
✔ Output: Working age
5. Multiple Conditions with or: day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
✔ Output: Weekend
6. Using not Operator: is_logged_in = False
if not is_logged_in:
print("Please log in")
✔ Output: Please log in
7. Nested if Statement: An if statement inside another if.
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive")
✔ Output: You can drive
8. Short-Hand if Statement: age = 20
if age >= 18: print("Adult")
✔ Output: Adult
9. Ternary One-Line if-else: age = 16
message = "Adult" if age >= 18 else "Minor"
print(message)
✔ Output: Minor
💡 Useful for simple conditions in one line.
10. Practice Examples: ✔ Check even or odd num = 8
if num % 2 == 0:
print("Even")
else:
print("Odd")
✔ Find the largest number a = 10
b = 20
if a > b:
print(a)
else:
print(b)
✔ Check positive or negative num = -5
if num >= 0:
print("Positive")
else:
print("Negative")
✔ Check password password = "python123"
if password == "python123":
print("Access Granted")
else:
print("Access Denied")
💡 Conditional statements make your programs intelligent by allowing them to make decisions based on different situations.
💬
Tap ❤️ if this helped you learn Python!