Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 09

Ternary Operator (if ... else)

Python does not use the C/Java ternary operator ?:. Learn Python's if ... else expression to choose one of two values.

Beginner12 min

Python does not use the C / Java ternary operator ?:.

Instead, Python uses the if ... else expression to choose one of two values.

Python ternary expression: Expr1 if Test else Expr2

What you'll learn

  • Python’s ternary syntax (Expr1 if Test else Expr2)
  • How the test picks exactly one branch
  • When a ternary replaces a short if / else assignment
  • That Python does not support ?:

Syntax

python
variable = Expr1 if TestCondition else Expr2

How it works:

  • if and else are Python keywords.
  • TestCondition must evaluate to True or False.
  • If the test is True, Python returns Expr1.
  • If the test is False, Python returns Expr2.
  • Only one of the two expressions is evaluated.

Visual flow

Ternary flow from test condition to assigned value

Example 1 — Adult or Minor

python
age = 20

status = "adult" if age >= 18 else "minor"

print(status)
text
adult

Since 20 >= 18 is True, Python chooses "adult".

Example 2 — Bigger Number

python
a, b = 10, 3

bigger = a if a > b else b

print(bigger)
text
10

The expression returns the larger value.

Example 3 — Even or Odd

python
n = 7

parity = "even" if n % 2 == 0 else "odd"

print(parity)
text
odd

The modulo operator % checks whether the remainder is zero.

Example 4 — Pass or Fail

python
marks = 34.56

result = "pass" if marks >= 40 else "fail"

print(result)
text
fail

Since 34.56 is below 40, Python chooses the else expression.

Interactive playground

Move the sliders and see the ternary expressions update live.

Move the sliders and see each ternary expression update live — same rules as Python in the playground below.

  1. 1. Adult or Minor

    status = "adult" if age >= 18 else "minor"

    Result

    adult

  2. 2. Bigger Number

    bigger = a if a > b else b

    Result

    14

  3. 3. Even or Odd

    parity = "even" if n % 2 == 0 else "odd"

    Result

    odd

  4. 4. Pass or Fail

    result = "pass" if marks >= 40 else "fail"

    Result

    fail

Ternary vs traditional if/else

Both programs produce the same result.

Traditional

python
if age >= 18:
    status = "adult"
else:
    status = "minor"

Ternary

python
status = (
    "adult"
    if age >= 18
    else "minor"
)

Use the ternary form when you are choosing between two values in a single assignment.

Practice exercises

Exercise 1 — Assign "pass" or "fail" using one ternary expression.

python
marks = 75

result = __________

Exercise 2 — Choose the smaller of two numbers.

python
a = 8
b = 12

smaller = __________

Exercise 3 — Rewrite this using a ternary.

python
if n > 0:
    sign = "positive"
else:
    sign = "negative"

Try your answers in the playground at the bottom of this lesson.

Quick knowledge check

  1. Does Python support ?:?
    No. Python uses if ... else.

  2. Which expression runs when the condition is False?
    Expr2 (the else part).

  3. Can the test be a logical expression?
    Yes. Any expression that evaluates to True or False can be used.

Summary

  • Python’s ternary syntax is Expr1 if Test else Expr2.
  • It chooses exactly one of two expressions.
  • It’s ideal for short value assignments.
  • Python does not support the C-style ?: operator.

Next module: String Handling — string methods, slicing, indexing, searching, replacing, and practical text-processing techniques.

Loading playground…