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 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,704 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: