Back
+10 XP
Concept

One-Line if/else (Ternary)

Don't waste 4 lines of `if/else` just to assign a simple variable. Use one clean line:

python
mode = "Dark" if dark_mode else "Light"

# 51. One-Line if/else (Ternary) Don't waste 4 lines of `if/else` just to assign a simple variable. Use one clean line: ```python mode = "Dark" if dark_mode else "Light" ``` Formula: `[value_if_true] if [condition] else [value_if_false]`. It reads like plain spoken English.

Checkpoint

What gets assigned to `status` here? ```python status = "Adult" if age >= 18 else "Minor" ```

Practice

Write a one-line ternary that sets `label = "Pass" if score >= 50 else "Fail"` for `score = 80`. Print `label`.

Terminal
01
Tap code blocks below to assemble
Available Blocks