How to Use the Ternary Operator in Python
Developers love concise code that’s easy to read, and that’s exactly what ternary operators are for. The ternary operator in Python lets you perform a small if/else statement in a single line. Let’s take a look at a few examples.
Example of selecting the larger number with a Pythonic ternary
bob_height = 6
jill_height = 7
larger_height = bob_height if bob_height > jill_height else jill_height
# larger_height = 7
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;
The formal structure of a ternary
Now that you’ve seen an example, it’s important to understand what’s going on. As you may have gathered from the name ternary, it takes 3 operands:
- true_val
- condition
- false_val
[true_val] if [condition] else [false_val]
true_val is returned if the condition is truthy, while false_val is returned if condition is falsy.
Example of checking if a number is oven or odd with a ternary
def is_even_message(num):
return "Number is even!" if num%2 == 0 else "Number is odd!"
print(is_even_message(6))
# Number is even!
print(is_even_message(5))
# Number is odd!
Example of a nested ternary
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 can do it if you really want to.
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")
Should you use ternaries in the Python programming language?
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.
Related Articles
Complete Guide to Removing Elements From Lists in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
While lists aren’t the most efficient data structure if you’ll be doing lots of deleting from the middle, there are definitely good ways to accomplish the task. The built-in remove() method should be your first option. Let’s go over some examples.
Removing Duplicates From a List in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
Let’s go over a few idiomatic ways to remove duplicates from lists in Python. Method #1 - Create a new list (simplest) This is the easiest algorithm to code, but because it requires creating a new list, also requires more memory and is a bit slower.
How to Check if a File Exists in Python
Dec 08, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
When working with files in Python, you’ll often need to check if a file exists before you do anything else with it, such as reading from or writing to it. Luckily, the Python standard library makes this a piece of cake.
Python vs C++: The Best Language To Learn For You
Nov 17, 2021 by Meghan Reichenbach
It’s either a blessing or a curse when choosing to learn Python or C++ because there couldn’t be two more opposing languages to compare.