November 21, 2021•3 min read

Branching with the Ternary Operator in Python

In Python the ternary operator shortens an assignment that involves an IF ELSE condition.

Read in Indonesian

Back again to branching in Python. If you already covered if, elif, and else earlier, this time you'll look at the short version: the conditional ternary operator. So what is it, and how do you use it? Let's go further...

About the Ternary Operator

A ternary operator is an operation in a programming language that needs 3 operands. No more and no less. Besides ternary there are also binary operators (2 operands) and unary operators (1 operand).

Whether you notice it or not, you have met binary operators many times. The most familiar ones are arithmetic, comparison, and boolean logic operations. So what about the ternary operator?

Python itself has several ternary operators, and one of them is the conditional ternary operator, whose job is to shorten an assignment that involves an if-else condition.

Format of the Conditional Ternary Operator in Python

As a ternary operator, this operation needs 3 operands: the main value, the condition, and the alternative value. It uses 2 keywords, if and else. Here is the format:

TEXT
<main-value> if <condition> else <alternative-value>
  • main-value: the value returned when the condition is met (True)
  • condition: the boolean condition used to decide which value is returned
  • alternative-value: the value returned when the condition is not met (False)

Using the Conditional Ternary Operator

Let's get into an example program. This one shows a passing grade based on the score a student got.

Take a look at the code below:

PYTHON
nilai = 90;
predikat = ""

if (nilai > 76):
  predikat = "lulus"
else:
  predikat = "tidak lulus"

print(predikat)

In that code you declare the variable nilai with the value 90, then a variable predikat that holds an empty string. Next you fill the value of the predikat variable according to the condition at hand. It gets "lulus" when the score is above 76, and "tidak lulus" when it is below that.

Declaring the predikat variable up front is optional, but it's better to do it so you avoid a possible undefined variable

The output is a sure thing, of course...

Now try the power of the ternary conditional operator to make that code shorter. With the same logic, the code can be changed into this:

PYTHON
nilai = 90
predikat = "lulus" if nilai > 76 else "tidak lulus"
print(predikat)

Look at that, only 3 lines now. Run the code and there should be no difference from before.

What is actually happening?

If you put the declaration of the predikat variable into human language, it comes out more or less like this.

TEXT
predikat is "lulus" if nilai is above 76, otherwise "tidak lulus"

See? It starts to make sense. With the human touch Python has, everything is easy to understand, ehehe...

At bottom, this ternary operator returns one of 2 possible values, according to the condition. That return value can be used straight away to run an operation.

So from here you can shorten the code again.

PYTHON
nilai = 90
print("lulus" if nilai > 76 else "tidak lulus")

Look at that, just 2 lines. If you want to see the result, just run it yourself.

Closing

That's all for this one, and I hope it's useful.

Next time you'll look at one more ternary operator in Python, namely list comprehension in Python 3.

See ya...

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.