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

Code Example

a, b = 5, 10

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

The code uses the ternary operator if a >= b else b to choose between two values based on a condition. If a >= b is true, a is printed, otherwise b is printed.

In this specific example, a is 5 and b is 10, so b is printed.