How can the ternary operators be used in python?

Answer Posted / chaitanya

The ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it. The operator will be given as:

[on_true] if [expression] else [on_false]

x, y = 25, 50

big = x if x < y else y

This is the lowest priority operator that is used in making a decision that is based on the values of true or false. The expression gets evaluated like if x<y else y, in this case if x<y is true then the value is returned as big=x and if it is incorrect then big=y will be sent as a result.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is python idle used for?

437


How do I sleep in python?

422


How is print statement represented in python 3 (v/s python2)?

492


What is python's parameter passing mechanism?

500


Is coding useful in finance?

443






How to redirect the output of a python script from standout(ie., A monitor) on to a file?

479


How do you reverse the string in python?

473


What is a namespace in python?

484


How python maintains conditional blocks?

427


What is the concatenation?

469


What is a function in python programming?

467


Is there polymorphism in python?

452


Explain other string literals?

463


What is cgi in python?

520


Is python good for coding?

473