Computer Science, asked by suvanshmahajan351, 11 months ago

Is there an equivalent of C’s ""?:"" ternary operator in Python ?

Answers

Answered by mahendrarajbhar83867
0

Answer:

Ternary Operator in Python. Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows to test a condition in a single line replacing the multiline if-else making the code compact.

Answered by letmeanswer12
0

Yes, Python has ternary operator

Explanation:

In python, the ternary operator is used to return a value dependent on a binary condition result. As an input, it takes a binary value(condition) so it looks like an "if-else" condition block. It also returns a value, however, so it behaves similar to a function. Python essentially evaluates the condition first, if valid–evaluating the first expression otherwise evaluates the condition second.

  • Syntax - [on_true] if [expression] else [on_false]

Example using ternary operator

  • >>> x, y = 5, 6
  • >>> print("x" if x> y else "y")
  • y

Similar questions