- ?:
Ternary operator (Conditional Operator)
examples/basics/ternary.py
x = 3 answer = 'positive' if x > 0 else 'negative or zero' print(answer) # positive x = -3 answer = 'positive' if x > 0 else 'negative or zero' print(answer) # negative or zero
examples/basics/without_ternary.py
x = 3 if x > 0: answer = "positive" else: answer = "negative or zero" print(answer) # positive x = -3 if x > 0: answer = "positive" else: answer = "negative or zero" print(answer) # negative or zero
In other languages this is the ?: construct.