if/else is a two-way fork. What if you have three or more options — say, classify a number as negative, zero, or positive?
Nest? if x < 0: ... else: if x == 0: ... else: ...?
That works but indentation grows fast and gets unreadable. Python has elif ("else if") for exactly this:
if x < 0:
print("negative")
elif x == 0:
print("zero")
else:
print("positive")Python checks each condition top to bottom and runs the first block whose condition is True. The rest are skipped, even if their conditions would also have been true.
And the else at the end?
Catches "none of the above". Optional — leave it off if you're fine with no branch running. With else, exactly one branch always runs.
How many elif can I chain?
As many as you need, though more than 4-5 starts to smell. Long chains are often better expressed with a dictionary lookup, but that's later. Three or four elif is normal.
elif — multi-way branchingThe shape:
if condition_1:
...
elif condition_2:
...
elif condition_3:
...
else:
...Python evaluates each condition in order. The first one that's True wins — its block runs, and everything below is skipped.
When ranges overlap, the order you write the conditions in changes which branch runs:
score = 95
if score >= 60:
print("pass")
elif score >= 90:
print("distinction") # never reached!score >= 60 matches first, so we never check the more specific >= 90. Write narrowest condition first:
if score >= 90:
print("distinction")
elif score >= 60:
print("pass")Given if/elif*/else, exactly one block always runs. Without the final else, zero or one — if no condition matches, no block runs and the program falls through.
temp = 22
if temp < 10:
label = "cold"
elif temp < 20:
label = "cool"
elif temp < 30:
label = "warm"
else:
label = "hot"
print(label) # warmNotice each elif only checks the new boundary — the previous range is already excluded by the conditions above failing.
Create a free account to get started. Paid plans unlock all tracks.
if/else is a two-way fork. What if you have three or more options — say, classify a number as negative, zero, or positive?
Nest? if x < 0: ... else: if x == 0: ... else: ...?
That works but indentation grows fast and gets unreadable. Python has elif ("else if") for exactly this:
if x < 0:
print("negative")
elif x == 0:
print("zero")
else:
print("positive")Python checks each condition top to bottom and runs the first block whose condition is True. The rest are skipped, even if their conditions would also have been true.
And the else at the end?
Catches "none of the above". Optional — leave it off if you're fine with no branch running. With else, exactly one branch always runs.
How many elif can I chain?
As many as you need, though more than 4-5 starts to smell. Long chains are often better expressed with a dictionary lookup, but that's later. Three or four elif is normal.
elif — multi-way branchingThe shape:
if condition_1:
...
elif condition_2:
...
elif condition_3:
...
else:
...Python evaluates each condition in order. The first one that's True wins — its block runs, and everything below is skipped.
When ranges overlap, the order you write the conditions in changes which branch runs:
score = 95
if score >= 60:
print("pass")
elif score >= 90:
print("distinction") # never reached!score >= 60 matches first, so we never check the more specific >= 90. Write narrowest condition first:
if score >= 90:
print("distinction")
elif score >= 60:
print("pass")Given if/elif*/else, exactly one block always runs. Without the final else, zero or one — if no condition matches, no block runs and the program falls through.
temp = 22
if temp < 10:
label = "cold"
elif temp < 20:
label = "cool"
elif temp < 30:
label = "warm"
else:
label = "hot"
print(label) # warmNotice each elif only checks the new boundary — the previous range is already excluded by the conditions above failing.