What are the ternary operator in Python. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What are the ternary operator in Python.

Does Python has ternary operator.

20th May 2021, 3:41 AM
Sagar Yadav
Sagar Yadav - avatar
13 Answers
+ 4
no there isn't ternary operator in python... however, you could do an if..else statement assignation in one line: value = val1 if condition else val2
20th May 2021, 3:45 AM
visph
visph - avatar
+ 2
visph Pardon me. I am actually a Mathematics college student. ;)
20th May 2021, 6:46 PM
Ore
Ore - avatar
+ 1
An operator that needs exactly three operands. I doubt it exists in Python. But there is a pythonic similitude of ternary operator(?:) that exists in other languages. It is the inline if else statement. value = option1 if condition else option2
20th May 2021, 3:53 PM
Ore
Ore - avatar
+ 1
Hmm. Our answers looks very different to me. 🤷
20th May 2021, 4:13 PM
Ore
Ore - avatar
+ 1
visph A ternary operator is an operator that requires exactly three operands. The ternary conditional operator in most language happen to be the only ternary operator in these languages. This is why people associate conditionals with ternary operators. By considering the true meaning of a ternary operator. Your answer seems a little off. The OP never asked directly if it is possible to write if...else statement in a single line.
20th May 2021, 6:12 PM
Ore
Ore - avatar
+ 1
visph If you are in the group of programmers that accepts SQL as a programming language. Then you must have heard of BETWEEN and NOT BETWEEN https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_between
20th May 2021, 6:23 PM
Ore
Ore - avatar
+ 1
Ore yeah, you're right... but I don't think that SQL could be considered as all other programming languages ^^ (but I've never asking to myself if it is or not... in fact, I don't think to SQL when I think to programming language rather much more as a query language ;P)
20th May 2021, 6:29 PM
visph
visph - avatar
+ 1
visph I see. That's fine. Speaking of other contexts in which ternaries apply. Ternary operations have their roots from mathematics. You probably already know about binary operations like addition and combination. You might also know of unary operations like factorial. But ternary operations also exist especially in geometry.
20th May 2021, 6:34 PM
Ore
Ore - avatar
+ 1
I don't know that ternary operators have their roots from maths: I only have basic scientific background... so appart of classical mathematics I don't understand caballistic signs such as used in geometry or combination ;P
20th May 2021, 6:44 PM
visph
visph - avatar
+ 1
Ore No problem: you let me learn something new ;)
20th May 2021, 6:47 PM
visph
visph - avatar
0
Ore that's just exactly what I said in the first post... with the difference that I'm sure it doesn't exist in python: that's a language design choice ;)
20th May 2021, 3:56 PM
visph
visph - avatar
0
'very' maybe not ^^ but it's true that you answer 'what' while I skip the title and answered only the question description: 'does python has ternary operator' ... as there is no ternary operator, the question title has no realistic response for me ('what are the ternary operator IN PYTHON') ;P
20th May 2021, 4:19 PM
visph
visph - avatar
0
Ore I agree with you, but could you show me another example of ternary operator (obviously in coding world, even if I doubt there are other context where they could apply ^^) ?
20th May 2021, 6:19 PM
visph
visph - avatar