Python – ternary conditional statement (if, or, else)

Ternary operator (Ternary operator) – used in line to set conditions in assigning a value to a variable. It is easier to understand this with examples.

cat_say = “Mew”

me_say = “Hi,cat” if cat_say == “Mew” or cat_say == “Myavki” else “Who are you?”

print(me_say)

Hi,cat #result

The ternary operator essentially includes the name of the variable to which we assign a value and inside the same line we set the conditions described by the if / or / else statements. It is not necessary to use all three if/or/else statements, just if/else can be used in the condition.

Important: The ternary conditional operator in Python can be used with both strings and numbers.