in Technology by
Does Python have a ternary conditional operator?

1 Answer

0 votes
by

The expression syntax is:

a if condition else b

First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

For example:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Related questions

0 votes
    8. Rewrite the following using ternary operator. (a) if (bill>10000) discount=bill*10.0/100; else discount=bill*5.0/100; Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Can someone tell me any string.contains or string.indexof method in Python I want to do if not somestring.contains("boom"): continue Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Which of the following is the truncation division operator in Python? a) | b) // c) / d) %...
asked Dec 31, 2022 in Technology by JackTerrance
0 votes
    How many operators are there under the category of ternary operators?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    How many operators are there under the category of ternary operators in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    ______ is used for Visualisation, verification and calibration of ternary probabilistic forecasts. (a) toss (b) ... R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    How does bitwise operator XOR works?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    How does bitwise operator XOR works in C programming?...
asked Nov 9, 2020 in Technology by JackTerrance
...