Concept Flow - Ternary conditional expression
Evaluate condition
Yes
Use value if True
End
No
Use value if False
End
The ternary expression first checks a condition. If true, it returns the first value; otherwise, it returns the second value.
x = 10 result = "Even" if x % 2 == 0 else "Odd" print(result)
| Step | Condition (x % 2 == 0) | Result | Value Chosen | Output |
|---|---|---|---|---|
| 1 | 10 % 2 == 0 | True | "Even" | "Even" |
| 2 | Print result | - | - | Even |
| Variable | Start | After Step 1 | After Step 2 |
|---|---|---|---|
| x | 10 | 10 | 10 |
| result | undefined | "Even" | "Even" |
Ternary conditional expression syntax: value_if_true if condition else value_if_false It evaluates the condition first. If True, returns value_if_true. If False, returns value_if_false. Useful for simple if-else in one line.