Python ternary operator: Definition and effective ways to implement it

As you may know, Python is one of the most simple and user-friendly programming languages in the world. In the previous blog, ArrowHiTech reviewed for you the Python advantages and disadvantages in detail. In order to continue the series of Python, the next article we want to discuss with you is about the Python ternary operator. What does it mean and what are the effective methods for you to master it? Then, let’s take a look with us right now! 

What is Python ternary operator?

Python ternary operator
Python ternary operator

In Python, if you want to write conditional statements, the ternary operator is the ideal choice for you. The ternary Python operator has three operands, as the name implies. 

Besides, the ternary operator is a one-line variant of the if-else statement that is used to test a condition.

definition of Python ternary operator
Diagramme of Python ternary operator


In a ternary operator, there are three operands:

  • condition: A boolean expression that returns true or false when evaluated.
  • true_val: If the expression is true, it will assign a value to it.
  • false_val: If the expression is evaluated as false, it will assign a value to it.

var = true_val if condition else false_val

The left-hand side of the = (assignment) operator’s variable var will be assigned:

  • If the booleanExpression evaluates to true, value1 is returned.
  • If the booleanExpression evaluates to false, value2 is returned.

Then, let’s look at the example below:


to_check = 6

msg = “Even” if to_check%2 == 0 else “Odd”



msg = “”

if(to_check%2 == 0):

  msg = “Even”


  msg = “Odd”


The ternary operator is used in the above code to determine if an integer is even or odd.

  • If the condition (to check percent 2 == 0) is true, msg will be assigned “even.”
  • If the condition (to check percent 2 == 0) is false, msg will be assigned “odd.”
what is Python ternary operator
Definition of Python ternary operator

Effective ways to implement Python ternary operator?

A tuple is another technique to do ternary operations in Python.

In fact, this is an easy alternative to the ternary operator if-else.

Then, let’s explore the following syntax:

(false_value, true_value)[conditional_expression]

As you probably see, the case below indicates the two members of the tuple are false_value and true _ value.

Instead of an index, the conditional_expression is enclosed in square brackets.

This works because True has a value of 1 and False has a value of 0.

As a result, you will get the outcomes:

  • The value of the conditional_expression becomes if it evaluates to True.
  • The ternary expression then returns the element at index 1.
  • The value of the conditional_expression becomes 0 if it evaluates to False.
  • After that, it will return the element with index 0.

Besides, you must make sure to place the false_value at index 0 and true_value at index 1 in the tuple. 

Then, let’s refer to the following syntax:

>>> a = 10

>>> b = 5

>>> largest = (b, a)[a > b]

>>> print(largest)

Therefore, you can gain the result below:

result of syntax

After that, you can explore the lines of code below:

>>> nice_weather = True

>>> print((“Watch a movie at home”, “Go out for a walk”)[nice_weather])

Then, the outcome will appear:

syntax of Python ternary operator

Rather than using a tuple, you are able to enhance this syntax and apply a list or a dictionary.

How to write syntax for Python ternary operator when using a list?

To begin, let’s have a glance at the syntax below and you will understand it with ease:

[false_value, true_value][conditional_expression]

Then, you should follow the following instance:

>>> a = 10

>>> b = 5

>>> largest = [b, a][a > b]

>>> print(largest)

As a result, the output will be shown like:

Python ternary operator
Python ternary operator when using a list

Way to write syntax for Python ternary operator when using a dictionary

First and foremost, let’s see the syntax below:

{True: true_value, False: false_value}[conditional_expression]

In this case, the true_value and false_value represent true value and false value, respectively. The value equivalent to True is returned if the conditional expression evaluates to True.

Otherwise, it will return the value associated with False.

In order to understand the Python ternary operator when using dictionary in detail, you can refer to the following instance:

>>> nice_weather = False

>>> s = {True: “Go out for a walk”, False: “Watch a movie at home”}[nice_weather]

>>> print(s)

Therefore, the output you get is:

outcome of Python ternary operator
Python ternary operator when using a dictionary

In conclusion

All in all, ArrowHiTech hopes via this article, you will dig out further helpful information in order to work well with Python ternary operator. If you are having any difficult issues, let contact us. ArrowHitech has become one of the superior IT outsourcing Vietnam companies. We are experienced professionals in the field of  ECommerce, Web/mobile apps, CMS website development as well as Salesforce and Software Consultant & Development.