Case Statements

Steven D'Aprano steve at pearwood.info
Wed Mar 16 08:56:26 EDT 2016


On Wed, 16 Mar 2016 09:34 pm, BartC wrote:


> (BTW why does Python have 'elif' when if-else is all that is really
> needed?)

To save indentation.


if condition:
    block
else:
    if other:
        block
    else:
        if third:
            block
        else:
            block


versus:

if condition:
    block
elif other:
    block
elif third:
    block
else:
    block


Python could have spelled "elif" as "else if", but that's just a matter of
spelling. Either way, the important thing is that the else-if/elif keeps
the same indentation as the if.



-- 
Steven




More information about the Python-list mailing list