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

Liam Hayes
seal-mask
.a{fill-rule:evenodd;}techdegree
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 Points

Using if's and else's inside a parameter parenthesis

Hey guys check out this code from Learn Python Track -> Python Collections -> Lists -> Shopping List Take Three.

Here it is:

def clear_screen():
    os.system("cls" if os.name == "nt" else "clear")

I actually understand what the function does, what I don't understand is the content inside the parenthesis: ("cls" if os.name == "nt" else "clear")

I'm not used to using parameters in this way. I'm used to putting values or variables in the parenthesis, but I've never seen if and else in there before. What does this mean?

Thank you!

2 Answers

andren
andren
28,558 Points

That is an example of a conditional expression, often called a ternary expression in other languages. Since it is an expression (something that will always evaluate to a value) and limited to one line it can be used anywhere you would place a regular value.

The syntax is basically like this:

VALUE_IF_TRUE if CONDITION else VALUE_IF_FALSE

When Python comes across a conditional expression it looks at the condition of the expression, if the condition evaluates to true then the expression evaluates to the first value provided, the value I named VALUE_IF_TRUE in my example. And otherwise it evaluates to the second value provided, the value I named VALUE_IF_FALSE in my example.

So in the case of the code you posted

"cls" if os.name == "nt" else "clear"

Python will check if os.name is equal to "nt" and if it is then the expression resolves to "cls" otherwise it resolves to "clear" instead. The important thing to keep in mind is that Python does this evaluation before it sends the argument to the function, so as far as the function is concerned you are just passing it a regular string.

Dan Garrison
Dan Garrison
22,457 Points

Here is the documentation on this particular method: https://docs.python.org/2/library/os.html#os.system

Basically, os.system allows you to execute subshell commands in your python code. The shell is something like Command Line or Power Shell on Windows, Terminal on Macs, Bash on linux systems. What this code is saying is that if the name of the operating system is NT (a really old version of windows) then you execute the command "cls" in the shell and if it is anything else then you run the command "clear". Both of these commands basically clear the screen of the shell you have open.

Liam Hayes
seal-mask
.a{fill-rule:evenodd;}techdegree
Liam Hayes
Full Stack JavaScript Techdegree Student 13,116 Points

Thanks for you comment Dan. I actually already understand what the function does. What I was wondering was about the "if" and the "else" inside the parenthesis. How can someone put that in a parenthesis for parameters? I'm used to a variable being a parameter, or a value being a parameter, but he put a whole line of code with if's and else's in there. How does that work? Do you happen to know?