Python Ternary Operator

In the last lesson, we learned about the Python match-case Statement. Now let’s shorten a simple two-choice if-else into one expression.

Let’s Talk About Shortcuts

We know how to use if-else.

age = 20
if age >= 18:
result = "Adult"
else:
result = "Minor"
print(result)

Output

Adult

That works. But it takes four lines. And honestly, it is a bit much for such a simple decision.

What if I told you there is a way to write the same thing in one line?

That is what the ternary operator is for.

What Is the Ternary Operator in Python?

The ternary operator is a short way to write an if-else statement.

Instead of four lines, we write one line.

It is called “ternary” because it uses three parts.

  • The condition
  • The value if true
  • The value if false

Three parts. Ternary. That is where the name comes from.

The Structure of the Python Ternary Operator

value_if_true if condition else value_if_false

Let me break it down.

  • First, the value we want if the condition is true
  • Then the word if
  • Then the condition
  • Then the word else
  • Then the value we want if the condition is false

It reads like English: “This if that, else this other thing.”

A Simple Python Ternary Example

Let’s rewrite the age example using the ternary operator.

age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)

Output

Adult

See? One line instead of four. Same result.

Let me show what happens step by step.

  • age >= 18 is checked. Is 20 >= 18? Yes.
  • So the value before if is used. That is "Adult".
  • The value after else is ignored.
  • result becomes "Adult".

If age was 15:

age = 15
result = "Adult" if age >= 18 else "Minor"
print(result)

Output

Minor

Because 15 >= 18 is False. So the value after else is used.

Python Ternary: Another Example — Even or Odd

number = 7
result = "Even" if number % 2 == 0 else "Odd"
print(result)

Output

Odd

Clean. Simple. One line.

Compare that to the regular if-else:

number = 7
if number % 2 == 0:
result = "Even"
else:
result = "Odd"
print(result)

Both work. The ternary version is just shorter.

Using the Ternary Operator Directly in print()

We can use the ternary operator directly inside print.

age = 20
print("Adult" if age >= 18 else "Minor")

Output

Adult

No need for a variable. Just print the result directly.

Using the Python Ternary Operator with Numbers

The values do not have to be strings. They can be numbers.

a = 10
b = 20
maximum = a if a > b else b
print(maximum)

Output

20

Because a > b is False (10 is not greater than 20). So b is used. And b is 20.

That is a nice way to find the maximum of two numbers.

Similarly, for minimum:

a = 10
b = 20
minimum = a if a < b else b
print(minimum)

Output

10

Python Ternary Operator: A Real-Life Example

Let’s say we are building a simple login system. We want to show a message based on whether the user is logged in.

is_logged_in = True
message = "Welcome back!" if is_logged_in else "Please log in"
print(message)

Output

Welcome back!

If is_logged_in was False:

is_logged_in = False
message = "Welcome back!" if is_logged_in else "Please log in"
print(message)

Output

Please log in

Clean and easy.

Another Example — Discount

is_member = True
price = 100
final_price = price * 0.9 if is_member else price
print(f"Final price: ${final_price}")

Output

Final price: $90.0

If is_member is True, apply 10% discount. Otherwise, full price.

That is the ternary operator in action.

Nested Ternary in Python

We can nest ternary operators. But let’s be careful. It gets messy fast.

score = 85
grade = "A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "F"
print(grade)

Output

B

It works. But it is hard to read. Can we tell what is happening at a glance? Not really.

Compare that to the if-elif-else version:

score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(grade)

This is much easier to read. Even though it is longer.

So my advice: do not nest ternary operators. Use if-elif-else instead.

The ternary operator is best for simple, two-choice decisions. Not for complex logic.

When to Use the Python Ternary Operator

Use the ternary operator when:

  • We have a simple if-else
  • Both branches just assign a value
  • The whole thing fits on one line and is easy to read

Do not use it when:

  • The logic is complex
  • We need multiple elif
  • The line becomes too long
  • It makes the code harder to understand

Readable code is more important than short code. Always.

Common Mistakes with Python Ternary Operator

Mistake 1: Wrong Order

result = if age >= 18 "Adult" else "Minor" # Error!

The order is wrong. The value comes first. Then the condition.

So what do we do? We put the value first.

result = "Adult" if age >= 18 else "Minor"

Mistake 2: Forgetting the else

result = "Adult" if age >= 18 # Error!

The ternary operator requires both parts. We cannot skip the else.

If we want an if without else, we use a regular if statement.

if age >= 18:
result = "Adult"

Mistake 3: Using It for Side Effects

print("Adult") if age >= 18 else print("Minor")

This works. But it is not good style.

The ternary operator is for choosing a value. Not for running actions.

Better:

if age >= 18:
print("Adult")
else:
print("Minor")

Or:

message = "Adult" if age >= 18 else "Minor"
print(message)

Mistake 4: Making It Too Long

result = "This is a very long message that goes on and on" if some_complicated_condition and another_condition else "This is another very long message"

This is technically valid. But it is unreadable.

So what do we do? We use a regular if-else.

A Complete Python Ternary Operator Example

Let’s build a simple ticket price calculator using the ternary operator.

age = int(input("Enter your age: "))
price = 5 if age < 12 else 10 if age < 65 else 7
print(f"Ticket price: ${price}")

Hmm. That nested ternary is hard to read. Let me rewrite it.

age = int(input("Enter your age: "))
if age < 12:
price = 5
elif age < 65:
price = 10
else:
price = 7
print(f"Ticket price: ${price}")

Much better. This is why we avoid nested ternary.

Now a simpler example where ternary works well:

is_student = input("Are you a student? (yes/no): ").lower() == "yes"
discount = 2 if is_student else 0
price = 10 - discount
print(f"Ticket price: ${price}")

Output if student:

Output

Ticket price: $8

Output if not student:

Output

Ticket price: $10

Clean. Simple. The ternary operator works perfectly here.

Next, we will learn about the Python for Loop and repeat code for every item in a sequence.