Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching If, Else and Elif

aditya verma
aditya verma
1,853 Points

Difference between elif and if

In the example, there are three possible names, couldnt we have written 'if' statement for Craig and Maximilian and else for the family member.

In short whats the difference between using elif versus if in the second codition of maximiliane?

1 Answer

Steven Parker
Steven Parker
231,275 Points

An "elif" condition is only tested if the previous "if" test was false. When you use another "if" instead of "elif", then the second condition will always be tested, no matter what the result of the first one was.

Proper use of "elif" will usually make the program more efficient, plus it allows you to use logical progressions that would not work with a plain "if". For example:

if value > 60:
    print "Large"
elif value > 30:     # a plain "if" would not work here
    print "Medium"
else:
    print "Small"

Substituting a plain "if" for the "elif" would cause a value over 60 to print out both "Large" and "Medium".