Ternary operator

The conditional (ternary) operator is often used as a short form of an if-else statement. This is simply a conditional assignment of a value to a variable. if-then-else, on the other hand, is a flow control.

expression1 if condition else expression2

Example: ternary operator

Code Explanation

LineDescription
1Initializes the variables
a with the value 5 and
b with value 10
3If a is greater than or equal to b, a is output, otherwise b
a, b = 5, 10

print(a if a >= b else b)
Output
10