Search in this blog

Wednesday, July 5, 2017

if...elif...else in Python


That’s the difference in Python between an if...elif...else and a series of ifs:
if a:
    do this
elif b:
    do that
elif c:
    do something else
else:
    print "none of the above"
is equivalent to (exactly the same as) this:
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
As you can see, the elif...else notation allows you to write the same logic (the same control flow) without repeating the a condition over and over again.

Copied from here: https://www.codecademy.com/en/forum_questions/51684a3d4ce76309b4001b9c