Back to articles list Articles
6 minutes read

Does Python Have a Ternary Conditional Operator?

Are you a Python programmer who loves writing clean and concise code? If so, you may wonder if Python has a ternary conditional operator. We’ll answer your question in this article.

Other programming languages have a ternary operator – a programming feature that lets you compare three conditions in one line. With Python’s insistence on code that’s clear, clean, and concise, you’d expect Python to have a ternary conditional operator as well. And that’s what we’ll be talking about in this article.

If you are not confident using Python yet, our Python Programming Track will help you get started. It’s a set of 5 interactive courses that will teach you Python's fundamentals: data types, functions, methods, control flow statements, and more. By the end of the track, you will be able to write your own functions and pursue the Python programming journey of your choice – whether that’s general programming, data science, or something else.

By the way, if data science interests you, check out our 12 Python tips and tricks that every data scientist should know.

But let's go back to our topic! Whether you're a seasoned Python developer or just starting out, this article will provide you with valuable insights into one of Python's most useful features. So, let's dive in!

What Is a Python Ternary Conditional Operator?

A ternary or conditional operator is a shorthand way of writing an if-else statement in a single line of code. It's often used in programming to make code more concise, especially when dealing with simple conditional expressions.

In C, a simple if-else statement could be written as:

if (x > 0) {
 y = 1; 
} else { 
 y = -1; 

Using the ternary operator, the same expression can be written as:

y = (x > 0) ? 1 : -1;

As you can see, the code is much shorter, especially when you’re dealing with simple conditional expressions.

Now, the question remains: does Python have a ternary operator?

The answer is yes; it is called a conditional expression rather than a ternary operator.

Indeed, Python does have a ternary conditional operator in the form of a conditional expression using if and else in a single line.

The syntax for a Python ternary conditional operator is:

value_if_true if condition else value_if_false

This code evaluates the condition. If it's true, the code returns the value of value_if_true; if it's false, it returns the value of value_if_false. It's important to note that the if and else are mandatory.

Let's look at an example to see how the Python conditional expression works:

The Python Conditional Expression at Work

>>> x = 10
>>> y = "even" if x%2 == 0 else "odd"
>>> print(y) # output: even

In this example, we use the Python conditional expression to assign the value of even to y if x is even and the value of odd if x is odd.

Note that we could write the same thing using an if-else statement, but it would be much longer.

>>> x = 10
>>> if x%2 == 0:
. . .   y = "even"
. . . else:
. . .   y = "odd"
>>> print(y)

# output: even

Python Ternary Operator with Multiple Conditions

We can also use a Python ternary operator with multiple conditions. In the case of a standard if-else statement, we would need to use elif:

>>> if x%2 == 0:
...   y = "even"
... elif x == 3:
...   y = "Hello Python!"
... else:
...   y = "odd"


To write the equivalent as a one-liner Python ternary operator, we need to ditch the elif keyword and use else instead; we are, in fact, writing a nested ternary operator. Therefore, the code would be:

>>> y = "even" if x%2 == 0 else "Hello Python!" if x == 3 else "odd"
>>> print(y)

And here we go! We have checked multiple conditions in one line of code with a Python ternary conditional operator!

Efficient Python Conditional Expressions

As you might realize by now, one of the main advantages of using Python's conditional expression syntax is that it can significantly reduce the amount of code needed to write a simple if-else statement. We can also use list comprehension to make the code even more efficient.

Let's say we need to print a list of items based on a particular condition. Here's an example of using Python's ternary operator to print a list of even and odd numbers with list comprehensions.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
# Using a conditional expression to print even and odd numbers 
even_numbers = [num for num in numbers if num % 2 == 0] 
odd_numbers = [num for num in numbers if num % 2 != 0] 
print("Even numbers:", even_numbers) if even_numbers else print("No even numbers found") 
print("Odd numbers:", odd_numbers) if odd_numbers else print("No odd numbers found")

In this example, we use list comprehension to create two separate lists of even and odd numbers from a given list of integers. We then use conditional expressions to print the even and odd numbers if they exist or print a message if no even or odd numbers are found.

Using Python's ternary operator, we can write more concise and readable code compared to standard if-else statements. This example demonstrates how conditional expressions can be a valuable tool in simplifying your code and making it more efficient. This is especially true when dealing with simple conditional expressions or nested conditions. Instead of writing a multi-line if-else statement, we can write a single-line conditional expression that achieves the same result.

Also, note that Python always skips the else statement when the if evaluates to True; this decreases the code execution's time.

Leveraging Python’s Ternary Conditional Operator

In summary, Python's conditional expression syntax can save a significant amount of space when dealing with simple conditional expressions. Still, it's essential to consider readability and efficiency when using nested or complex conditions.

Now that you've learned about Python's ternary operator, it's important to practice using it in your code. By incorporating conditional expressions into your code, you can make your code more concise and efficient.

A great way to practice using Python's ternary operator is by trying it out in our Built-In Algorithms in Python course. You can also look at existing code and identify areas where a ternary operator could be used instead of a standard if-else statement.

Remember, while Python's ternary operator can make your code more concise, it's important to prioritize readability and maintainability. So, use it appropriately and consider alternative options for more complex conditions.

With practice, using Python's ternary operator can become second nature and significantly shorten your code. Adding knowledge of VS Code extensions for Python and useful Python packages to your toolbox will bring you to the next level!

See you soon on for more Python tips!