Conditional Expressions (Ternary)

A conditional expression — Python's ternary — is the one-line form a if condition else b that evaluates to a when the condition is true and b otherwise.

Learn Conditional Expressions (Ternary) in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

It's perfect for choosing between two values inline: setting a default, labeling a number, or transforming items inside a comprehension — all without a multi-line if/else block.

The value you usually want comes first, then the condition, then the fallback. It reads almost like a sentence:

Read it as: "Give me adult , if age is 18 or more , otherwise minor ."

A ternary lets you transform every item in a comprehension based on a condition. Here the if/else goes at the front (transform), not the trailing filter position:

You can chain ternaries to mimic if/elif/else , but readability drops quickly. Compare these grade examples:

Replace each ___ to complete the ternary expressions so the output matches.

✅ Python has no ?: . Write status = "adult" if age >= 18 else "minor" .

✅ A conditional expression must have an else . (A trailing if with no else is only for filtering inside comprehensions.)

✅ Ternaries are for choosing a value , not running statements. If each branch does work, use a real if/else block.

Use ternaries to format counts with correct grammar and a friendly default.

Go deeper with the official Python documentation:

Lesson complete — one-liners with confidence!

You can choose between two values with a if cond else b , drop it into f-strings and comprehensions, and you know when to stop nesting and switch to a full if/elif/else .

🚀 Up next: The Walrus Operator (:=) — assign and use a value in the very same expression.

Practice quiz

What is the correct syntax for a Python conditional expression?

  • condition ? a : b
  • if condition then a else b
  • a if condition else b
  • a unless b if condition

Answer: a if condition else b. Python's ternary is 'a if condition else b' — the value comes first, then the condition, then the fallback.

What does "adult" if age >= 18 else "minor" evaluate to when age is 20?

  • "adult"
  • "minor"
  • True
  • 20

Answer: "adult". Since 20 >= 18 is true, the expression evaluates to the first value, "adult".

Why is a ternary called an expression rather than a statement?

  • Because it must be on one line
  • Because it cannot contain conditions
  • Because it always returns None
  • Because it produces a value you can assign, return, or pass

Answer: Because it produces a value you can assign, return, or pass. A conditional expression evaluates to a value, so you can assign it, return it, or drop it into a call or comprehension.

What does [x if x % 2 == 0 else 0 for x in [1,2,3,4]] produce?

A leading ternary transforms every item, replacing odd numbers with 0: [0, 2, 0, 4].

What is the difference between a leading and a trailing if in a comprehension?

  • A leading if/else transforms every item; a trailing if filters items
  • They are the same
  • A leading if filters; a trailing if/else transforms
  • Only trailing ifs are valid

Answer: A leading if/else transforms every item; a trailing if filters items. [x if c else y for x in xs] transforms each item; [x for x in xs if c] filters items out.

How does the chained ternary "A" if s>=90 else "B" if s>=80 else "C" group?

  • Left to right
  • It is a syntax error
  • Right to left, like if/elif/else
  • It evaluates all branches first

Answer: Right to left, like if/elif/else. Nested ternaries chain right-to-left, mimicking if/elif/else — but readability drops fast beyond one level.

Is the else branch required in a conditional expression?

  • No, it is optional
  • Yes, a conditional expression must have an else
  • Only inside comprehensions
  • Only when assigning to a variable

Answer: Yes, a conditional expression must have an else. A ternary always needs an else. A trailing if with no else is only valid as a comprehension filter.

What does the C-style status = age >= 18 ? "adult" : "minor" cause in Python?

  • It works the same as the ternary
  • A TypeError
  • It returns a boolean
  • A SyntaxError — Python has no ?: operator

Answer: A SyntaxError — Python has no ?: operator. Python has no ?: operator; that C-style syntax is a SyntaxError. Use 'a if cond else b' instead.

When should you choose a full if/elif/else over a ternary?

  • When choosing between two simple values
  • When each branch needs multiple statements or its own logic
  • Never — ternaries are always better
  • Only inside functions

Answer: When each branch needs multiple statements or its own logic. A ternary holds one expression per branch; use a full block when branches need multiple statements or side effects.

What does abs_x = x if x >= 0 else -x compute when x is -5?

  • -5
  • 0
  • 5
  • None

Answer: 5. Since -5 >= 0 is false, the expression takes -x, which is -(-5) = 5 — an inline absolute value.