How to use the Python Ternary Operator

This tutorial takes you through how to use the ternary operator in the Python programming language.

How to use the Python Ternary Operator

Python’s ternary operator allows you to execute a statement based on whether a condition is true or false. This operator is essentially the same as an if-else statement, but can be kept to a single line and be more readable.

A ternary operator or conditional expression will work best when you want to reduce a traditional if-else statement to a single line. However, it is essential not to overuse this operator as it can sometimes make code harder to read.

Throughout this tutorial, we will take you through several different aspects of the ternary operator. First, we will discuss the syntax and go through a few different examples of using the operator.

This tutorial is based on Python 3.0 and later. Previous versions may not have the functionality described in this tutorial, as ternary operators were only added in Python 2.5. We recommend upgrading to the latest version of Python as soon as possible.

Syntax of the Python Ternary Operator

The syntax of a ternary operator in Python differs slightly from other languages such as PHP, C++, JavaScript, and more. Unfortunately, this difference can lead to Python novices making mistakes when using the operator.

Our ternary operator works by first evaluating the condition and then running true_val or false_val depending on the result of the condition.

var = true_val if condition else false_val
  • var is the variable that will store the ternary operator’s result. This variable is optional.
  • true_val is where you specify the return value for when the condition return true.
  • condition is your conditional expression that will evaluate to either true or false.
  • false_val is the return value for when the condition returns false.

Using the Ternary Operator in Python

In this section, we go through different ways you can use the ternary operator in your Python scripts. It’s important to note that this tutorial is written for Python 3.0 or later. In previous versions of Python, this functionality may not exist.

Basic Usage of the Ternary Operator in Python

In this example, we will show you how to use the ternary operator and provide an explanation on each piece of code.

We first declare a variable x and give it the value of 5.

Next, we declare a variable called “result“, which we will store the result from our ternary operator. As for our ternary operator, we have our “true value“, condition, and lastly, our “false value“. (var = true_val if condition else false_val)

The condition for the ternary operator states that if x is greater than 10, the result is true. If x is not greater than 10, the result will be false.

Lastly we use the print function to output our result to the terminal.

x = 5

result = "x is greater than 10" if x > 10 else "x is less than 10"
 
print(result)

After you run the code above, you should have a result like the one below. Changing the value of the x variable to higher than 10 will change the output.

py ternary.py
x is less than 10

Using print with the Ternary Operator

You can output the result of the ternary operator straight into the print function rather than into a variable. Using the print function directly will further reduce the amount of code you need and can make the code easier to read.

Our example below demonstrates how you can use the print function with the ternary operator.

x = 5

print("x is greater than 10" if x > 10 else "x is less than 10")

Running the code above will output the same result as our other ternary operator examples.

py ternary.py
x is less than 10

Example Written as a Regular if Statement

If we take the ternary operator above and turn it into the equivalent if-else statement, you will get something similar to our example below.

Both methods will produce the same result, but the ternary operator will allow you to reduce the code from four lines to one.

Reducing the amount of code can help with readability in some cases but not all. So, be mindful of when to use an if-else statement and when to use a ternary operator.

x = 5

if(x > 10):
    result = "x is greater than 10"
else:
    result = "x is less than 10"
 
print(result)

As expected, the if-else statement produces the same output as our ternary operator.

py ternary.py
x is less than 10

Conclusion

I hope this tutorial has helped you understand how to use the ternary operator in Python. It is great for writing short, readable conditional statements. However, it is important not to overuse ternary operators as they may have the opposite effect in terms of code readability.

We have plenty more Python tutorials that are great for introducing you to the many basics of the language. For example, you may be interested in understanding variables better or how to use a logical operator.

Please let us know if you notice a mistake or an important topic is missing from this guide.

Leave a Reply

Your email address will not be published. Required fields are marked *