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?”
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.

This entry was posted in Python (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.