The ternary operator is a way to concisely say:
“If test, then a, else b“,
with the value of the statement being the value of a or b.
|language||how to say it|
|C||test ? a : b|
|C++||test ? a : b|
|Perl (not perl 6)||test ? a : b|
|PHP||test ? a : b|
|Ruby||test ? a : b|
|Julia||test ? a : b|
|Did I forget some language?||probably|
|Python||a if test else b |
Ok. Now that I’ve written this post, I’ll remember it.
However, I just want to say…
On behalf of all of the other multiple-language programmers in the world, …
THIS IS LAME!!!
Well, there are some reasonable reasons.
As noted in the comments, the Python version is more readable (at least in English).
And, to tell you the truth, if I ONLY were programming in Python, I’d probably prefer the Python version.
I understand the reasoning behind Python’s version.
I JUST DON’T AGREE WITH IT!
Glad I’ve gotten that off my chest.