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.
Python does not use the C / Java ternary operator ?:.
Instead, Python uses the if ... else expression to choose one of two values.
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/elseassignment - That Python does not support
?:
Syntax
variable = Expr1 if TestCondition else Expr2How it works:
ifandelseare Python keywords.- TestCondition must evaluate to
TrueorFalse. - 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
Example 1 — Adult or Minor
age = 20
status = "adult" if age >= 18 else "minor"
print(status)adultSince 20 >= 18 is True, Python chooses "adult".
Example 2 — Bigger Number
a, b = 10, 3
bigger = a if a > b else b
print(bigger)10The expression returns the larger value.
Example 3 — Even or Odd
n = 7
parity = "even" if n % 2 == 0 else "odd"
print(parity)oddThe modulo operator % checks whether the remainder is zero.
Example 4 — Pass or Fail
marks = 34.56
result = "pass" if marks >= 40 else "fail"
print(result)failSince 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. Adult or Minor
status = "adult" if age >= 18 else "minor"
Result
adult
2. Bigger Number
bigger = a if a > b else b
Result
14
3. Even or Odd
parity = "even" if n % 2 == 0 else "odd"
Result
odd
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
if age >= 18:
status = "adult"
else:
status = "minor"Ternary
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.
marks = 75
result = __________Exercise 2 — Choose the smaller of two numbers.
a = 8
b = 12
smaller = __________Exercise 3 — Rewrite this using a ternary.
if n > 0:
sign = "positive"
else:
sign = "negative"Try your answers in the playground at the bottom of this lesson.
Quick knowledge check
-
Does Python support
?:?
No. Python usesif ... else. -
Which expression runs when the condition is False?
Expr2 (theelsepart). -
Can the test be a logical expression?
Yes. Any expression that evaluates toTrueorFalsecan 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…