Python ternary operator: When to use if-elif-else instead

Python ternary operator: When to use if-elif-else instead

Python’s ternary operator is convenient, but easy to abuse:

‘A’ if s>=90 else ‘B’ if s>=80 else ‘C’ if s>=70 else ‘F’

Just because you CAN, doesn’t mean you SHOULD!

For 3+ conditions, use if-elif-else. Save the ternary for comprehensions, lambdas, and return statement.