Does Python have a ternary conditional operator?


Yes, Python has a ternary conditional operator, which is a shorthand way of writing an if-else statement. The syntax for a ternary conditional operator in Python is:

[on_true] if [expression] else [on_false]

For example, the following code:

x = 10
y = 20

max = x if x > y else y
sets the variable max to the value of x if x is greater than y, and sets it to the value of y otherwise. The value of max in this case would be 20.
Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.