The ternary operator was introduced in Python from version 2.5. You can write ternary operator in Python using below syntax –
1 |
Here is a simple example of ternary operator in Python –
1 2 3 4 5 6 7 8 |
a = 1 b = 2 1 if a > b else -1 # Output is -1 1 if a > b else -1 if a < b else 0 # Output is -1 |