Does Python have a ternary operator? If so, how does it work?
A ternary operator, otherwise known as a conditional expression, is a simple if-else statement that is performed on one line of code to evaluate a condition. The value entered before the if
clause is the True
value returned, whereas the value after the else
clause is the False
value returned. The expression between the if
and the else
is the conditional clause that determines what value is returned.
In Python the ternary operator has the following schematic:
|
|
Here’s how this would work in the Python REPL:
|
|
As you can see from the simple example above the ternary operator performs a simple check on whether the my_age
variable will have a remainder of 1 when divided by 2 (the %
modulo division returning the remainder of the division). In this case as the number 45
is an odd number this evaluates to True
and therefore the value_if_true
part of the ternary operator is returned.
Here’s what would have happened if the number was even:
|
|
As the condition in the expression evaluates to False
in this example the expression after else
is returned.
How Do You Assign A Ternary Operator To A Variable?
To assign the result of a ternary operator in Python to a variable simply declare the name of the variable before the ternary operator followed by the assignment symbol =
.
Something like the following:
|
|
From the above example, the code has changed a little so the ternary operator can return the essential ingredient needed: determining whether to output "odd"
or "even"
. As you can also see from this ternary operator it is assigned to the variable odd_or_even
and this variable is then used in an f-string to help output the intended result.
Can You Use A Ternary Operator Without else
?
Can you use the ternary operator syntax without the else
clause? By its nature, a ternary operator needs to have both the result to return if True
and the result to return if False
.
If you’re looking to create an expression with just an if
statement then the only alternative would be something that would have the following schema:
|
|
Unfortunately, it isn’t deemed to Pythonic as the nature of if
statements in Python is the result is inserted on a new line, like so:
|
|
But the single line if statement does work, as demonstrated below:
|
|
As you can see the Python REPL requires a second carriage return after entering that second line containing the if
statement. It isn’t something recommended, but can be done.
What Is A Ternary Operator Alternative?
What can you use instead of the ternary operator? Is there something similar in Python that can be used?
In older versions of Python a way to perform the ternary operator was using tuples. Here’s an example of the format of that tuple:
The only problem with this approach is that the value_if_false
isn’t true it will still run.
Here’s an example demonstrating the problem with this approach:
|
|
As you can see both expressions print out, this could easily be fixed by moving the print
statement outside like so:
|
|
However, if you don’t want the False
side of the tuple to evaluate or run then the syntax will need a little tweak by inserting lambda expressions. Using the previous example where print
functions were used in both tuple elements, let’s prefix these with lambda
statements are run again:
|
|
Notice this time that the REPL didn’t output the print
from the False
tuple (being the first element, or the element with an index of 0
)?
Therefore, to use this syntax if there is an expression that could evaluate and impact your code with the non-returning side then prefix each of the tuple elements with the lambda:
expression.
Here’s the syntax using lambda
:
Summary
The ternary operator provides Python developers with the ability to code a conditional expression using a single line of code that is succinct and easy to follow.
If you’re looking to use the ternary operator in your code follow the format: value_if_true if condition else value_if_false
.
If you’re looking to use an alternative that uses tuples use the format: (lambda: value_if_false, lambda: value_if_true)[condition]()
.
Do remember both approaches have different ordering of the True
and False
values, so it might be best to use one approach consistently throughout your code.