Home > Software engineering >  Difference Between ELIF and Nested If in Python?
Difference Between ELIF and Nested If in Python?

Time:12-10

Attempting to learn Python, but slightly confused. When would I use a nested IF statement over an ELIF? The course instructor provided the top code while my code is written on the bottom. The purpose of the program is to determine if a year is or is not a leap year:

instructor

year = int(input("Which year do you want to check? "))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print("Leap year.")
        else:
            print("Not a leap year.")    
    else:
        print("Leap year.")
else:
    print("Not leap year.")

mine

year = int(input("Which year do you want to check? "))

if year % 4 == 0:
    print("Leap year.")
elif year % 100 == 0:
    print("Leap year.")
elif year % 400 == 0:
    print("Leap year.")
else:
    print("Not leap year.")

CodePudding user response:

elif is short for else if. The elif will only be evaluated if no prior if or elif in the chain was true.

Your version of the code doesn't work because any year that is divisible by 4 will execute the first block; none of the elifs can ever be true, and the else: print("Not leap year.") can't happen once that initial if is true.

year = int(input("Which year do you want to check? "))

if year % 4 == 0:
    print("Leap year.  (Maybe!)")
elif year % 100 == 0:
    print("This will never get executed because 100 is a multiple of 4.")
elif year % 400 == 0:
    print("This will also never get executed for similar reasons.")
else:
    print("Not leap year.")
Which year do you want to check? 1904
Leap year.  (Maybe!)
Which year do you want to check? 2000
Leap year.  (Maybe!)
Which year do you want to check? 2001
Not leap year.
Which year do you want to check? 2100
Leap year.  (Maybe!)

The output of the instructor's code for the same inputs is:

Which year do you want to check? 1904
Leap year.
Which year do you want to check? 2000
Leap year.
Which year do you want to check? 2001
Not leap year.
Which year do you want to check? 2100
Not a leap year.

Note the "not a leap year" result for 2100 (which is correct).

CodePudding user response:

Any time you flatten nested conditionals like your instructor's you're essentially anding all of the previous levels to each condition:

if A:
    if B:
        if C:
            foo
        elif D or E:
            42
        else:
            bar
    else:
        baz
else:
    wooble

Starts to look like:

if A:
    if A and B:
        if A and B and C:
            foo
        elif A and B and (D or E):
            42
        elif A and B:
            bar
    elif A:
        baz
else:
    wooble

And then we get rid of all but the last if, and compress the indentation to a single level:

if A and B and C:
    foo
elif A and B and (D or E):
    42
elif A and B:
    bar
elif A:
    baz
else:
    wooble

The nested conditional can be cleaner since we're not repeating conditions, but it can also be more difficult to follow logically, whereas the single level example makes every condition very explicit.

Translating your instructors code using the same method:

if year % 4 == 0:
    if year % 100 == 0:
        if year % 4 == 0 and year % 100 == 0 and year % 400 == 0:
            print("Leap year.")
        elif year % 4 == 0 and year % 100 == 0:
            print("Not a leap year.")    
    elif year % 4 == 0:
        print("Leap year.")
else:
    print("Not leap year.")

Cleaning it up:

if year % 4 == 0 and year % 100 == 0 and year % 400 == 0:
    print("Leap year.")
elif year % 4 == 0 and year % 100 == 0:
    print("Not a leap year.")    
elif year % 4 == 0:
    print("Leap year.")
else:
    print("Not leap year.")

But knowing math we know that we can remove redundant checks.

if year % 400 == 0:
    print("Leap year.")
elif year % 100 == 0:
    print("Not a leap year.")    
elif year % 4 == 0:
    print("Leap year.")
else:
    print("Not leap year.")

And there's the same check done in one level of indentation.

CodePudding user response:

If you were to use elifs instead of nested ifs, then you should order the conditions in order of specificity.

year = int(input('year'))

if year % 400 == 0:
    print('Leap')
elif year % 4 == 0 and year % 100 != 0:
    print('Leap')
else:
    print('Not Leap')

Seeing that two branches have the same outcome, we can or the conditions together to simplify.

if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
    print('Leap')
else:
    print('Not Leap')
  • Related