elif
`elif` adds another condition to an `if`: it is checked only if the earlier ones were false.
Used in 33 lessons of the course, from 00.02.
elif is short for "else if". It goes after an if and before an optional else.
Python checks the conditions from top to bottom. When one is true, it runs that block and skips the rest. That is why order matters: put the most specific condition first.
You can chain as many elif as you need. Each ends with : and has its own indented block.
if x < 0:
...
elif x == 0:
...
else:
...
In the course you will see it used to pick between several paths, for example depending on the kind of value a function receives. A chain of elif is clearer than several separate ifs, because it shows that only one path runs.
Reach for it when: you have more than two possible paths and only one should run.
Examples
Common mistake
score = 95
if score > 60:
print("pass")
elif score > 90:
print("excellent")Why: The first condition is already true, so the `elif` is never checked. Order matters.
How to write it: Put the stricter condition first: if score > 90: and then elif score > 60:.
