elif
Short for else if: an extra case in an if statement, tested only when every earlier condition was false.
In more detail
Python checks the conditions in order and runs only the first true one, then skips the rest, so order matters: put the strictest test first. A final else catches anything left; without it, nothing may happen.
Example
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"Related terms
- if statement — A statement that runs a block of code only when a condition holds, optionally with an else block for when it does not.
- condition — An expression that is tested for being true or false, such as weight > 50, to decide what a program does next.
Taught in
- Literals, Identifiers And Keywords — Hands-on Python: Complete Video Course & Book
- If Elif Chains — Hands-on Python: Complete Video Course & Book
- Nesting Control Flow Statements — Hands-on Python: Complete Video Course & Book
- Loops And Tuples — Hands-on Python: Complete Video Course & Book
- Branching With If, Elif And Else — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Control Flow Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Functions, Return Values And Recursion — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep