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 Collections (2016, retired 2019) Lists Shopping List Take Three

Youssef Moustahib
Youssef Moustahib
7,779 Points

Different ways of using if and else?

In this video Kenneth makes the clear screen function, in previous videos he did the same but he did it as:

def clear_screen():

if os.name == 'nt:
    os.system('cos')
else:
    os.system('clear')

But In this video Kenneth does it a completely different way and didn't explain it. He wrote it all in one line, all in one pair of brackets? Could someone please explain this way of doing it please? Thanks

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Youssef,

I see that you're new to Treehouse, but I'm not sure of your coding experience. What Kenneth is using here isn't actually an if statement, even though it uses "if" and "else". This is something that is taught a bit further on into the language, once you get to more Intermediate courses, so a complete explanation goes a bit beyond the Scope of this Post.
In short, however, that line is called a Conditional Expression, sometimes referred to as a Ternary Operator by other languages. Have a look at the documentation, but also know that this will be covered in time.

Glad to see you are noticing differences in code and are eager to know more!! :thumbsup:

Keep Coding! :) :dizzy: