What Are Python Ternary Operators and How Do You Use Them?
A Python ternary operation (aka a conditional expression) is a means to evaluate a condition and return a value, based on if the condition is True or False. With this operator, you can write an if-else statement with a single line. By doing this, your Python code is more concise.
You would use a ternary operator for simple conditions so readability isn’t compromised, but you would avoid them for complex conditions or nested ternary operators, which can lead to confusion and affect code clarity. When you need to assign a value to a variable based on a single condition and you want to keep your code short and sweet, a ternary operator might be your best option.
The ternary operator syntax is very easy to follow and looks something like this:
|
1 |
value_if_true if condition else value_if_false |
Okay, let’s take a look at an example of the ternary operator. Here’s a very simple example (this time, I’ll add comments for clarity):
If you run the above code, it will print out:
Adult
Why? Because the ternary operator checks age against our condition. (if it’s greater than or equal to 18) and sets the variable status. If the condition is true, it prints Adult, if the condition is false, it prints Minor.
It’s that simple.
Okay, let’s take this one step further and ask for user input. This time around, we’ll assign a That code (with comments) looks like this:
Notice we had to use the int() function for our ternary operator. Why? Because if we don’t, we get a TypeError of ‘>=’ not supported between instances of ‘str’ and ‘int’
When we run the above code, we’ll be asked for our age and then the ternary operator will check the input against the condition. The results from the condition will then be printed. When the value is true, the ternary operator returns value_if_true and if the value is false, it returns value_if_false.
If we wrote the above as an if…else statement, it would look something like this:
If we run the above code, we’d get the same results as we did with the ternary operator. The difference is that the ternary operator option is far more concise and readable.
In the end, that’s what it’s all about.
You can also use a ternary operator in Nested if…else statements. The syntax for this is:
|
1 |
value_if_true if condition1 else (value_if_true if condition2 else value_if_false) |
Let’s take a look at a sample of a nested ternary operator. This time we’ll simply compare two numbers, assigned to variables a and b. The code looks like this:
Can you guess what the result would be?
B is a larger number
In the above example, the first ternary operator is “The numbers are equal” if a == b else “A is a larger number” which is nested with the second ternary operator, a > b else “B is a larger number,” in an if…else statement
With Tuples
Ternary operators can also be used with tuples. Remember, a tuple is a collection of items or values that are immutable, which means they cannot be changed once they are created.
The syntax for using tuples with ternary operators looks like this:
|
1 |
(value_if_false, value_if_true) [condition] |
Let’s use the same example as above to compare two numbers, using a tuple instead. That code looks like this:
What happened? Remember the syntax which lists the false value first, followed by the true value, followed by the condition. What we’ve done is set our condition to state that a > b. Since a = 100 and b = 150, the condition is false, so the false statement is printed, which is B is the larger number.
With Dictionaries
A dictionary in Python is a key:value pair. With regards to our ternary operator, that would look something like this:
|
1 |
True: value_if_true, False: value_if_false |
Let’s stick with our same example.
The output of the above would be:
B is the larger number
And that, my Python-wielding friends, is how you use a ternary operator to keep your code clean and concise.