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?
Similar Posts
- Install dependencies using pip from requirement.txt file in Python
- [Python] Using comprehension expression to convert list of dictionaries to nested dictionary
- [Python] Create a list of objects using class names
- [Python] Get nodeid, module, function name using Pytest request built-in fixture
- [Python] Using inspect.stack() to get module name, path, function name
- Join two videos using moviepy in Python
- Get tofixed of a value using round() Python