Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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!