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

can someone explain what else , if and else if does?

can someone explain what Else , if , and elif means a little more

THank you

1 Answer

Alexander Besse
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 Points

Hi Jamshaid Ali!

If is the beginning of a conditional statement. Think of it in English like "If there are eggs in my fridge -> cook them". You can only have one If statement in the chain of If/Elif/Else. Therefore there is Elif (Short for Else If), which acts just like If but says "None of the If/Elseif above me passed, do I pass? Else is a "Nothing passed, so I want to do this instead."

if eggs in fridge:
    eggs.eat()
elif toast in pantry:
    toast.eat()
elif coffee in coffee_pot:
    coffee.drink()
else: #Nothing passed, default to this one.
    orderDelivery()

I hope that helps. Here is a good article to read:

https://realpython.com/python-conditional-statements/

Happy coding!