Language Syntax
Conditional expression — Python syntax
Evaluate the condition first and only the selected result expression. Nested conditional expressions associate to the right; use parentheses to clarify complex cases.
Formal Syntax & Structure
python syntaxpython
true_value if condition else false_valueParameters & Attributes
| Name | Type | Description |
|---|---|---|
| true_value | expression | Evaluated only when condition is truthy. |
| condition | expression | Evaluated before either branch. |
| false_value | expression | Evaluated only when condition is falsy. |
Return Value
object
Value obtained by evaluating this expression; see description for context and constraints.
Working Example
Example Snippetpython
n=0print(10/n if n else 'empty')