Lesson 6 of 40Article14 min
If/Else & Match
if, elif, and else choose code paths based on conditions. Always indent the block after the colon.
Python If...Else
if, elif, and else choose code paths based on conditions. Always indent the block after the colon.
Combine conditions with and, or, and not.
Real-life example: If/else is a gatekeeper — if you have a ticket, you enter; else you wait outside.
Grade checker
marks = 86
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
else:
grade = "C"
print(grade)Python Match
match/case (Python 3.10+) compares a value against patterns — cleaner than long elif chains.
Use _ as the default case wildcard.
Real-life example: match is a vending machine menu — press A for chips, B for soda, default for cancel.
match/case
status = "success"
match status:
case "success":
print("Done!")
case "error":
print("Failed.")
case _:
print("Unknown.")