0
What Is Ternery Opretors
3 Antworten
+ 4
hi, it is an if... else... conditional expression in a single line. The general description is:
a if condition else b
Example:
state = True if inp.upper() == inp[::-1].upper() else False
this code has an input inp that should be checked if it is a palindrome (reads the same forward as backward) In this case the input string inp is compared against the reversed string. If it does match - the var state will get True. If not it will get False.
+ 3
A ternary operator is ?:
In a program it is used as:
SomeVariable = condition?Option1:Option2
Here in the given example, the condition is checked and if it is true, the Option1 is executed otherwise Option2 is executed and the result is stored in SomeVariable.
Hope it helps 😁👍
0
It is an alternative way to write an if-else part on the fly:
if(a<b)
result = a;
else
result = b;
This becomes:
result = a<b? a: b;