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
but
is just so much nicer.
Thanks to ign for the docs reference:
To avoid brace^H^H^H^H^Helse if war.
In C/C++ where you have an
else if
, you can structure your code in many different styles:by having an
elif
instead, such war would never happen since there is only one way to write anelif
. Also,elif
is much shorter thanelse if
.