Home » Chapter 3 : Decisions
In-line if (a.k.a. Ternary) Statements
The in-line if statement evaluates one or more conditions and returns an output. The in-line if is also known as the Ternary Operator.
The general form of the inline if (Ternary Operator) statement looks like this:
[true_result] if [condition(s)] else [false_result][None]
Notice that the entire statement is on one line. Here's how it works:
Form | Traditional Syntax | In-Line Syntax |
---|---|---|
if |
|
|
if-else |
|
|
if-elif-else |
|
|
Write an inline if statement that prints "Yes" if a variable (x) equals 10.
Write an inline if statement that prints "Yes" if a variable (x) equals 10 or "No" if the variable does not equal 10.
Write an inline if statement that prints "Even" if the value of a variable contains an even number, or prints "Odd" if the value is odd.