views:

358

answers:

7

Hi!

I just started python programming!
There's one thing I wondered about, the "elif" keyword.

Any other programming languages I used before use simply the "else if" syntax.
Does anyone have an idea why the python developers add the additional "elif" keyword?

Why not:

if a:
    print("a")
else if b:
    print("b")
else:
    print("c")
+3  A: 

Most likely its syntactic sugar. Like the Wend of Visual Basic.

Daniel A. White
+12  A: 

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.

Anna Lear
That makes sense, since Python is indentation-sensitive.
zneak
@Anna: what about `else if` where the `else` and `if` are in the same line? I think that is what the OP meant.
Manoj Govindan
+1 : indentation = number of cases you want to check
Anurag Uniyal
@Manoj Govindan: exactly! (simply "else if ...:" instead of "elif ...:")
youllknow
@Manoj Govindan, `else if`, is invalid syntax in python, so do you mean 'else: if:` which is still invalid, do you mean instead of elif `else if` would have been better?
Anurag Uniyal
I guess the python compiler would simply treat the `else` in `else if` as a single keyword, so combined keywords are not possible..
poke
It *would* be possible, since the parser needs lookahead anyway.
delnan
@Anurag: I'm aware that `else if` is illegal in Python; an I wasn't saying `else if` would have been better than `elif`. I was merely trying to point out what I thought the OP was trying to ask: why not `else if` rather than `elif`. The OP seems to agree with my reading of his(?) intention.
Manoj Govindan
"else if" isn't always a single keyword. For example, in C#, it's just a nested if similar to how I wrote it out in my first example. It's just that C# doesn't care about whitespace as much as python, so it's valid to write out "else if" on one line. As for why it's called "elif" instead of "elseif", "elsif", or anything else, I have no idea. Languages are different and just because some do things one way, doesn't mean everybody has to follow suit. :)
Anna Lear
+2  A: 

Python inherits this from Perl, where it's called elsif.

In Python's case, else if as two separate constructs like it is in C-like languages would be quite ugly as you'd have to have else: if: with two indenting levels.

It's arguable whether special-casing the two keywords together would be better (so making else if a single construct, like the not in operator.

PL/SQL also has elsif, and the C preprocessor has it spelled elif.

bobince
"Python inhertis this from Perl" - Wat?
delnan
I think they both got it from preceding languages - Python is not at all a descendant of Perl.
Mark Snidovich
`s/Perl/Bash/` would have been better since in bash it really is `elif`. And I believe bash inherited it from sh which is much older than Perl.
slebetman
'elif' was in Algol 68 (http://en.wikipedia.org/wiki/ALGOL_68)
Andrew Dalke
+3  A: 

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:

if (...) {
    ...
} else if (...) {
    ...
}


if (...) {
    ...
} 
else if (...) {
    ...
}

if (...) {
    ...
} else 
if (...) {
    ...
}

// and so on

by having an elif instead, such war would never happen since there is only one way to write an elif. And also, elif is much shorter than else if.

Lie Ryan
the last example is painful to look at. Woe upon whomsoever inflicted this on you're innocent eyes.
TokenMacGuy
+2  A: 

That's just the way it is. Javascript uses else if, php uses elseif, perl uses elsif, the C preprocessor and python use elif. None of them are wrong, they just choose slightly different syntax to do the same thing. :D

CrazyJugglerDrummer
A: 

elif is some sort of replacement for switch in other languages but with more power

for example in C you write

switch (number){
 case 1:
   doA()
   break;
 case 2:
   doB()
   break;
 case N:
   doN()
   break;
 default:
   doSomethingElse()
   break;
}

in Python you write

if number == 1: doA()
elif number == 2: doB()
elif number == N: doC()
else: doSomethingElse()

As you see elif is more powerful since you can put more complex statements than in a switch, plus avoid nesting if/else statements

masterLoki
A: 

else if in python called elif

if a:
    print "a" 
elif b:
    print "b" 
else:
    print "c" 
Gunslinger_
OP knows this. He stated it in his question, and is asking why it is that way.
vlad003