Learn the coding skills for your next job

How to Use the Ternary Operator in Python

Developers love concise code that’s easy to read. A ternary operator in Python is a piece of syntax that lets you perform a small if/else statement in a single line. Let’s take a look at a few examples.

Selecting the larger number with a ternary

bob_height = 6 jill_height = 7 larger_height = bob_height if bob_height > jill_height else jill_height
Code language: Python (python)

You’ll notice that a ternary in Python actually looks a lot like a normal if/else statement, just jammed into one line. That’s basically what it is, except it also returns a value.

Contrast that syntax with JavaScript’s ternary, which feels a little different.

const largerHeight = bobHeight > jillHeight ? bobHeight : jillHeight;
Code language: JavaScript (javascript)

Structure of a ternary in Python

Now that you’ve seen an example, it’s important to understand what’s going on.

[on_true] if [expression] else [on_false]
Code language: CSS (css)

on_true is returned if the expression is truthy, while on_false is returned if expression is falsy.

Learn Python, get hired

Python has been growing in popularity for over 20 years, and today is used by tens of thousands of companies to deliver reliable software. If you are interested in a career in Python get started by writing real code with us.

Nested ternary in Python

First of all, I need to get this off my chest: please don’t nest your ternaries! It’s confusing and hard to read. That said, here’s how you would do it.

my_account = 100 wifes_account = 200 print("We have the same" if my_account == wifes_account else "I have more" if my_account > wifes_account else "Wife has more")
Code language: Python (python)

Should you use ternaries in Python?

Generally speaking, yes. Ternaries are fantastic little bits of syntactic sugar that make code more concise and readable when used sparingly. You should probably never nest ternaries, or try to use them with anything more than a simple assignment operation. By trying to use ternaries too often, your code becomes very hard for others (or yourself) to understand later.

Trying to find your next programming job?

If you are a self-taught developer having trouble finding your first programming job, we've got your back! We have the learning resources and tight-knit dev community that you need to land the coding job you've been looking for. To get started, create a free account and join our Discord community.

Have questions or feedback?

If we've made a mistake in the article, please let us know so we can get it corrected!