elif — Advanced Examples
Short for 'else if'; adds another condition to an if chain
elif vs match (Python 3.10+)
When to use elif vs structural pattern matching.
python
# elif for value ranges def classify_temp(t): if t < 0: return "freezing" elif t < 20: return "cold" elif t < 30: return "warm" else: return "hot" # match for structural patterns def process_command(cmd): match cmd.split(): case ["quit"]: return "Quitting" case ["go", direction]: return f"Going {direction}" case ["pick", "up", item]: return f"Picking up {item}" case _: return f"Unknown: {cmd}" print(classify_temp(25)) print(process_command("go north")) print(process_command("pick up sword"))
Expected Output
warm Going north Picking up sword
Use elif for simple value/range checks. Use match/case for destructuring complex data structures.
Want to try these examples interactively?
Open Advanced Playground