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
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.
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]

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?
0 Comments