Ternary operator in python

Ternary Operators

Ternary Operators are your classic if-then-else statements, but expressed in a single line. For example:

Can be translated to:


An if-then-else statement MUST have an else argument, otherwise your code will crash. You can also nest these statements; however, it is best practice to simply write the if statements separately if you have more than one expression.

Leave a Reply

Your email address will not be published. Required fields are marked *