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 (2015) Logic in Python If This Then That

how would i use elif ?

What does elif mean ?

Hi Jude, For instance,

age = 20

if age > 30: - for this line, you can call it condition 1

    print('good')  

elif age > 15: - for this line, call it condition 2

    print('very good') 

So the system will check two conditions you've set, first check condition 1, weather age > 30, if true, it will show word "good", if false, it will go to check condition 2, in this case, age is 20, it is false, so system will keep going to check condition 2, according to condition 2, age should greater than 15, yes, it is(because age = 20), so it is true, you will get result "very good"

2 Answers

Chris Schultz
seal-mask
.a{fill-rule:evenodd;}techdegree
Chris Schultz
Python Web Development Techdegree Student 19,775 Points

It is a shortened version of else if that you might be familiar with in other languages.

It must be used after an if statement and can be used more than once. It will need to have a condition like an if statement and unlike an else statement.

if a > b:
    return a
elif a < b:
    return b
elif a == b:
    return c
else:
    return d