I just started Python programming, and I'm wondering about the elif
keyword.
Other programming languages I've used before use else if
. Does anyone have an idea why the Python developers added the additional elif
keyword?
Why not:
if a:
print("a")
else if b:
print("b")
else:
print("c")
Far as I know, it's there to avoid excessive indentation. You could write
if x < 0:
print 'Negative'
else:
if x == 0:
print 'Zero'
else:
print 'Positive'
but
if x < 0:
print 'Negative'
elif x == 0:
print 'Zero'
else:
print 'Positive'
is just so much nicer.
Thanks to ign for the docs reference:
The keyword
elif
is short for 'else if', and is useful to avoid excessive indentation.