In Python, you can write an if-else
statement in a single line using the ternary conditional operator. Here’s the general syntax:
value_if_true if condition else value_if_false
Example:
x = 10
result = "Positive" if x > 0 else "Negative"
print(result)
This checks if x
is greater than 0 and assigns “Positive” to result
if true, or “Negative” if false.
Let me know if you’d like further examples!