Python's Ternary Conditional Operator

Python's Ternary Conditional Operator

The advantage to having a ternary conditional operator is that it allows for shorter if/else statements, which can more easily be included in-line in other code blocks. Python is no different from other programming languages in that it also has a ternary conditional operator.

The syntax is as follows:

>>> [expression1] if [condition] else [expression2]

As you can see, this shortens the if/else statement to a single line, which normally would have been 4 lines:

if [condition]:
   [expression1]
else
   [expression2]

For a more concrete example, consider the following example:

The ternary operator can be used in many cases, like conditionally executing an expression, or even assigning a variable based on a condition.

from datetime import datetime

halloween = datetime(2022, 10, 31)
today = datetime.today()

print('Happy Halloween!') if today.date() == halloween.date() else print('Nothing to celebrate...')

Assigning a variable based on a condition is a common use case for the ternary operator.

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

from datetime import datetime

new_years = datetime(2023, 1, 1)
today = datetime.today()

message = 'Happy New Year!' if today.date() == new_years.date() else 'Nothing to celebrate...'

While these are very simple examples, the ternary operator can be a very powerful feature.

One thing to be careful about is the order of operations in your expression. Since the ternary operator allows your expressions to be on the same line as the condition, it can lead to some unexpected results.

For example:

>>> a = 1
>>> b = 4
>>> c = 10 + a if a > 5 else b

Looking at this code, you might expect the result to be 14, but it actually returns 4. The first expression is grouped together, so Python sees it as 10 + a, instead of just a as you might expect. Because of this, the result is 4. If you want the ternary operator to just return 4 and then add it to 10, use parentheses to group the expression:

>>> a = 1
>>> b = 4
>>> c = 10 + (a if a > 5 else b)
>>> c
14

Unfortunately, there are some limitations with this syntax as it's not simply a one-line if/else statement. It does not support multiple expressions using the elif keyword. If you need to evaluate multiple expressions, you'll need to nest another ternary operator.

>>> a = 1
>>> b = 4
>>> c = 10 + (a if a > 5 else (b if b > 5 else 11))
>>> c
21
Last Updated: August 16th, 2022
Was this helpful?
Project

Real-Time Road Sign Detection with YOLOv5

# python# machine learning# computer vision# pytorch

If you drive - there's a chance you enjoy cruising down the road. A responsible driver pays attention to the road signs, and adjusts their...

David Landup
David Landup
Details
Project

Data Visualization in Python: The Collatz Conjecture

# python# matplotlib# data visualization

The Collatz Conjecture is a notorious conjecture in mathematics. A conjecture is a conclusion based on existing evidence - however, a conjecture cannot be proven....

David Landup
Jovana Ninkovic
Details

© 2013-2022 Stack Abuse. All rights reserved.

DisclosurePrivacyTerms