Python, why elif keyword?

raisyn picture raisyn · Sep 18, 2010 · Viewed 37k times · Source

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")

Answer

Adam Lear picture Adam Lear · Sep 18, 2010

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.