Search code snippets, questions, articles...

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

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]

Search Index Data (The code snippet can also be found with below search text)

ternary operator in python
single line if else condition in python
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet