Module 5 · Conditional Statements — topic 8 of 10
Conditional expressions
The one-line form value_if_true if condition else value_if_false produces a value from a decision. Learn its syntax, good uses inside assignments and f-strings, and when an if statement is clearer.
Learning objectives
- Write conditional expressions
- Use them inside assignments, arguments and f-strings
- Decide when a full if statement reads better
Definition
The one-line form value_if_true if condition else value_if_false produces a value from a decision. Learn its syntax, good uses inside assignments and f-strings, and when an if statement is clearer.
Introduction
A conditional expression chooses between two values in a single line: status = "adult" if age >= 18 else "minor". Other languages call this the ternary operator. In Python it reads almost like English.
Why it matters
Many small decisions only pick a value: a label, a plural ending, a default. Writing four lines of if-else for each clutters the code. A conditional expression keeps such choices compact while remaining readable.
Detailed explanation
The form is A if condition else B. Python evaluates the condition first; if it is true the whole expression becomes A, otherwise B. Only the chosen side is evaluated.
It is an expression, so it can appear anywhere a value is allowed: on the right of =, as a function argument, inside an f-string or inside a list comprehension.
The else part is mandatory; a conditional expression must always produce a value.
Expressions can be chained (A if c1 else B if c2 else C), but more than one level quickly becomes hard to read. Use if-elif-else instead.
If either branch needs to do several things (print, update variables, call functions for their effects), use a normal if statement. Conditional expressions are for choosing values, not for running actions.
The order of the parts differs from many languages that write condition ? a : b. In Python the likely or normal value usually comes first, which makes the line read naturally: price = member_price if is_member else normal_price.
Syntax
- The condition sits in the middle.
- Both values are required.
- Only the selected value is evaluated.
Code examples
Choosing a label
minor
Inside an f-string
1 item in cart 3 items in cart
More examples
Values in calculations and comprehensions
[49, 0, 0, 49] [499, 1200, 999, 129] Highest order 1200 is above 1000
Step-by-step explanation
- The comprehension uses a conditional expression to choose a shipping fee for each order.
- zip pairs each order with its fee so they can be added.
- Orders of 999 or more get free shipping, so 999 stays 999.
- The f-string chooses the word above or below in place.
Expected output
Running “Values in calculations and comprehensions” prints exactly:
Practical applications
- User interfaces pick singular or plural labels with conditional expressions.
- Pricing code selects a fee or zero in a single assignment.
- Templates choose CSS classes such as "active" if selected else "".
Best practices
- Use conditional expressions for short value choices.
- Avoid nesting them more than once.
- Switch to if-elif-else when the choice has more than two outcomes.
Common mistakes
Leaving out else
Avoid
Prefer
A conditional expression must always produce a value.
Using it for actions
Avoid
Prefer
Use statements for side effects; the expression form is for choosing values.
Key takeaways
- A if condition else B evaluates to A or B.
- It is an expression usable anywhere a value is.
- Use it for values, not for actions.
Try it yourself
- 1Assign "pass" or "fail" to a variable with a conditional expression based on marks.
- 2Print a list of numbers 1–10 where multiples of 3 are replaced by "Fizz" using a comprehension and a conditional expression.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?