Last updated: Apr 9, 2024
Reading timeยท4 min
Use a nested ternary operator to implement an if-elif-else statement on one line.
The first ternary should check for a condition and if the condition is not
met, it should return another ternary that does the job of an elif/else
statement.
my_str = 'bobby hadz' result = ("Anonymous" if not my_str else my_str.upper() if len(my_str) > 2 else my_str.capitalize()) print(result) # ๐๏ธ 'BOBBY HADZ'
The ternary operator is very similar to an if/else
statement.
name = 'Bobby Hadz' result = 'James Doe' if not name else name print(result) # ๐๏ธ 'Bob'
The example checks if the name
variable is falsy and if it is, the string
"James Doe" is returned, otherwise, the name
variable is returned.
To have an inline if-elif-else statement, we have to use a nested ternary.
my_num = 50 result = 10 if my_num > 100 else 20 if my_num < 100 else 0 print(result) # ๐๏ธ 20
You can wrap the statement in parentheses to make it more readable.
my_num = 50 result = (10 if my_num > 100 else 20 if my_num < 100 else 0) print(result) # ๐๏ธ 20
The first ternary in the example checks if the variable stores a value greater
than 100
.
If the condition is met, the number 10
gets returned.
else
statement runs and the nested ternary operator gets returned.The nested ternary operator checks if the variable is less than 100
.
If the condition is met, the number 20
is returned, otherwise, 0
is
returned.
Here is another example.
my_num = 50 result = ('a' if my_num > 100 else ('b' if my_num < 100 else 'c')) print(result) # ๐๏ธ 'b'
If the condition isn't met, the else
statement runs and the nested ternary
checks for another condition.
elif/else
statement.The nested ternary checks if the variable stores a value of less than 100
and
if the condition is met, the string b
gets returned. This is the elif
statement.
If the condition isn't met, the else
statement runs and the string c
gets
returned.
Here is how we would implement the ternary operator of the example using
if/elif/else
statements.
my_num = 50 if my_num > 100: result = 'a' elif my_num < 100: result = 'b' else: result = 'c' print(result) # ๐๏ธ 'b'
Using if-elif-else
statements is a bit more readable, but it is also a bit
more verbose.
Whether using a nested ternary operator makes your code more readable depends on the complexity of the conditions you are checking for.
Using the shorthand syntax isn't always recommended.
The ternary operator can also be used if you need a shorthand if-else statement.
The ternary operator will return the value to the left if the condition is met,
otherwise, the value in the else
statement is returned.
variable1 = 50 variable2 = 100 result = 5 if variable1 > variable2 else 10 print(result) # ๐๏ธ 10
The ternary operator is very similar to an if/else
statement.
The operator in the example checks if variable1
is greater than variable2
.
5
is returned, otherwise, the else
statement returns 10
.Here is the same code sample but using the longer form syntax.
variable1 = 50 variable2 = 100 if variable1 > variable2: result = 5 else: result = 10 print(result) # ๐๏ธ 10
a
if condition else b
The syntax of the ternary operator is a if condition else b
.
# ๐๏ธ 'hello' print('hello' if len('hi') == 2 else 'bye')
You can also store the result in a variable.
result = 'hello' if len('hi') == 2 else 'bye' print(result) # ๐๏ธ result
Here is another example.
name = None result = 'bobby hadz' if not name else name print(result) # ๐๏ธ bobby hadz
The example checks if the name
variable is falsy and if it is the string
"bobby hadz" is returned, otherwise the name
variable is returned.
I've also written an article on how to check for multiple conditions in an if statement.
You can learn more about the related topics by checking out the following tutorials: