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_value

Parameters & Attributes

NameTypeDescription
true_valueexpressionEvaluated only when condition is truthy.
conditionexpressionEvaluated before either branch.
false_valueexpressionEvaluated 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')
Python Reference