13 Réponses
+ 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
+ 2
visph Pardon me. I am actually a Mathematics college student. ;)
+ 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
+ 1
Hmm. Our answers looks very different to me. 🤷
+ 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.
+ 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
+ 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)
+ 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.
+ 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
+ 1
Ore No problem: you let me learn something new ;)
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
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 ^^) ?