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.
General Form
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:
[true_result]: This is the expression that will be evaluated and returned if the [condition] is true.
[condition]: This is (are) the boolean condition(s) that the ternary operator checks and that drives the result.
[false_result]: This is the expression that will be evaluated and returned if the [condition] is false.
[None]: There is no direct equivalent of the simple if statement (thst is, an if with no else clause) in the inline if statement. So, to represent the simple if in ternary, we must include else None instead.
Forms of In-Line if Statements
We saw on previous pages in this chapter that decisions are written as if, if-else, and if-elif-else statements. We can write these decisions structures as in-line if statements as well. Here is table that compares the structures side-by-side.
Form
Traditional Syntax
In-Line Syntax
if
x = 10
y = 20
if x > y:
print("x is greater than y")
'''
NOTE: There is no direct equivalent of the simple if statement
(that is, an if with no else clause) in the inline if statement.
So, to represent the simple if in ternary, we must include
else None (as shown below) instead.
'''
x = 10
y = 20
print("x is greater than y") if x > y else None
if-else
x = 10
y = 20
if x > y:
print("x is greater than y")
else:
print("x is not greater than y")
x = 10
y = 20
print("x is greater than y") if x > y else print("x is not greater than y")
if-elif-else
x = 10
y = 20
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
else:
print("x equals y")
x = 10
y = 20
print("x is greater than y") if x > y else (print("x is less than y") if x < y else print("x equals y"))
Practice Problems
Problem 1
Write an inline if statement that prints "Yes" if a variable (x) equals 10.
Python Code
x = 10
print("Yes") if x == 10 else None
Output
Yes
Solution Notes:
Code Line 1: First we need a value for our [condition] to evaluate. Here we declare a variable, x, and initialize it to the value of 10.
Code Line 2: This is our ternary statement. Our [true_result] is the print("Yes") part and our [condition] is the if x == 10. And, as indicated above, ternary operator statements require an else clause, so here we've added else None to fulfill that requirement.
Summary: Based on the [condition], if the value stored in x equals 10, then this will print "Yes." If x holds any other value the else None executes, which effectively does nothing so there will be no output at all.
Problem 2
Write an inline if statement that prints "Yes" if a variable (x) equals 10 or "No" if the variable does not equal 10.
Python Code
x = 10
print("Yes") if x == 10 else print("No")
Output
Yes
Solution Notes:
Code Line 1: First we need a value for our [condition] to evaluate. Here we declare a variable, x, and initialize it to the value of 10.
Code Line 2: This is our ternary statement. Our [true_result] is the print("Yes") part and our [condition] is the if x == 10. Our [false_result] is the print("No") part.
Summary: Based on the [condition], if the value stored in x equals 10, then this will print "Yes." If x holds any other value the else part executes, which will print No.
Problem 3
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.
Python Code
x = 10
print("Even") if x % 2 == 0 else print("Odd")
Output
Even
Solution Notes:
Code Line 1: First we need a value for our [condition] to evaluate. Here we declare a variable, x, and initialize it to the value of 10.
Code Line 2: This is our ternary statement. Our [true_result] is the print("Even") part and our [condition] is the if x is an even number. Our [false_result] is the print("Odd") part.
Summary: Based on the [condition], if the value stored in x is even which we determine using the modulus operator %, then this will print "Even." If x is an odd numer then the else part executes, which will print Odd.