To simulate construct of other language does Python have a ternary conditional operator

0 votes
If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
Oct 15, 2018 in Python by findingbugs
• 4,780 points
651 views

1 answer to this question.

0 votes

You can index into a tuple:

(falseValue, trueValue)[test]

test needs to return True or False.
It might be safer to always implement it as:

(falseValue, trueValue)[test == True]

or you can use the built-in bool() to assure a Boolean value:

(falseValue, trueValue)[bool(<expression>)]
answered Oct 15, 2018 by findingbugs
• 4,780 points

Related Questions In Python

0 votes
0 answers

Does Python have a ternary conditional operator?

If Python doesn't have a ternary conditional ...READ MORE

Apr 19, 2022 in Python by Edureka
• 13,620 points
465 views
0 votes
0 answers

Does Python have a ternary conditional operator?

Is there a ternary conditional operator in ...READ MORE

Sep 21, 2022 in Python by Samuel
• 460 points
445 views
0 votes
0 answers

What does a ternary conditional operator do in python?

Can you give an example using a ...READ MORE

Jun 17, 2019 in Python by Waseem
• 4,540 points
653 views
0 votes
0 answers
0 votes
1 answer

Does Python have a ternary conditional operator?

Think of the conditional expression as switching ...READ MORE

answered Nov 2, 2020 in Python by Gitika
• 65,770 points
578 views
0 votes
2 answers
0 votes
1 answer

what does a conditional operator in python do?

Conditional operators in python is same as ...READ MORE

answered May 2, 2019 in Python by Mohammad
• 3,230 points
1,391 views
+4 votes
6 answers

Does Python have a ternary conditional operator like C does?

Yes, the syntax is: a if condition else ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
2,477 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
4,903 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP