Python offers developers with more than one syntax to write the same code. Shorthand if else is one such feature using which you can write if else statement in one line of code. The format of shorthand if else is:
statement1 if condition else statement2
If the condition evaluates to True, then statement 1 will execute; otherwise, statement2 will run.
There is another variant of shorthand if else in which you can write more than one condition. The format of it is:
statement1 if condition1 else statement2 if condition2 else statement3
If condition1 evaluates to True, then statement 1 will execute, but if condition1 results in False, condition2 is checked. If condition2 evaluates to True, then statement2 executes; otherwise, statement3 executes.
a = 20 b = 15 print("a is maximum.") if a>b else print("b is maximum.")
a is maximum.
a = 10 b = 15 print("a is maximum.") if a>b else print("Both are equal.") if a==b else print("b is maximum.")
b is maximum.