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

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.