Python · expressions

Ternary Operator

An if/else that fits inside one expression: pick one of two values.

The shape

value_if_trueifconditionelsevalue_if_false
1. Python evaluates the condition first 2. True: the result is the left value 3. False: the result is the right value

Basic ternary


        # value_if_true if condition else value_if_false
        age = 20
        status = "Adult" if age >= 18 else "Minor"
        # ans: status is "Adult"
      
"Adult"ifage >= 18else"Minor"

Ternary with numbers


        # ternary with numeric values
        x = 10
        result = "Big" if x > 5 else "Small"
        # ans: result is "Big"
      
"Big"ifx > 5else"Small"

Nested ternary


        # ternary inside ternary
        score = 85
        grade = "A" if score >= 90 else "B" if score >= 80 else "C"
        # ans: grade is "B"
      
"A"ifscore >= 90else"B"ifscore >= 80else"C"

Read it left to right: the first True condition picks the value just before it.

Ternary in an assignment


        # use in variable assignment
        x = 5
        y = x * 2 if x > 3 else x + 2
        # ans: y is 10 (the condition is True)
      
x * 2ifx > 3elsex + 2

Ternary in print


        # use directly in print
        x = 10
        print("Even" if x % 2 == 0 else "Odd")
        # ans: Even
      
"Even"ifx % 2 == 0else"Odd"

Exercises

Predict the value, then press → to see which side is picked.

Exercise 1


        # what is result?
        x = 10
        result = "Big" if x > 5 else "Small"
        # ans: "Big"
      
"Big"ifx > 5else"Small"

Exercise 2


        # what about this?
        x = 0
        y = x if x else 10
        # ans: 10 (x is falsy)
      
xifxelse10

Exercise 3


        # nested ternary?
        score = 75
        grade = "A" if score >= 90 else "B" if score >= 80 else "F"
        # ans: "F" (it fails both conditions)
      
"A"ifscore >= 90else"B"ifscore >= 80else"F"

Exercise 4


        # ternary with a calculation?
        x = 5
        y = x * 2 if x > 3 else x + 2
        # ans: 10 (the condition is True)
      
x * 2ifx > 3elsex + 2

Exercise 5


        # what is message?
        is_valid = True
        message = "Valid" if is_valid else "Invalid"
        # ans: "Valid"
      
"Valid"ifis_validelse"Invalid"

Exercise 6


        # tricky: what happens?
        x = 5
        y = 10 if x > 3 else 20 if x > 1 else 30
        # ans: 10 (the first condition is True)
      
10ifx > 3else20ifx > 1else30

Exercise 7


        # what is assigned?
        x = None
        y = x if x is not None else "default"
        # ans: "default"
      
xifx is not Noneelse"default"

Exercise 8


        # boolean ternary?
        x = True
        result = 1 if x else 0
        # ans: 1
      
1ifxelse0

Exercise 9


        # what is the value?
        x = -5
        abs_x = x if x >= 0 else -x
        # ans: 5
      
xifx >= 0else-x

Exercise 10


        # what happens?
        x = []
        result = "Full" if x else "Empty"
        # ans: "Empty"
      
"Full"ifxelse"Empty"

Exercise 11


        # complex condition?
        x, y = 5, 10
        result = "Yes" if x > 0 and y > 0 else "No"
        # ans: "Yes"
      
"Yes"ifx > 0 and y > 0else"No"

Remember

Condition in the middle

a if cond else b: the condition is evaluated first.

Always both parts

The else is required: the expression must produce a value either way.

Chains read left to right

The first True condition wins. Keep nesting shallow so it stays readable.