python

Using ternary operator in Python

You can use ternary operator in python as below example shown

condition_value = 1
val = 1 if condition_value == 1 else 2
print val
Output
1

Ternary operators are useful to assign the condition-based return value. You can use ternary operators in python by using the below syntax

[if_condition_true] if [if_condition] else [if_condition_false]
Was this helpful?