Modern Programming
Modern Programming
Modern Programming
Just send pseudo code to AI and compile straight to binary.
Peak programming
I love something = condition and result1 or result2
in lua
Python does that, too.
https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
often I do a function called elvis XD with the next signature elvis(condition, res1, res2)
Yeah... I played that "serial killer or programming language inventor" game.
The only one I was completely in disagreement with was the inventor of Python. He's definitely a mass-murderer
Are you sure it isn't just that he's Dutch?
num % 2
isn't a boolean result in any of these languages, so I feel like it would always output "odd"
Edit: 0 is false, everything else is true.
In JS at least, there's a concept of truthiness and falsiness. 0
, undefined
, null
, and a few other non-boolean values are treated as false
if used in conditionals and logical operations, while every other value is treated as true
. I'm pretty sure python has something similar.
It does. Empty collections, 0, None
0 is false in C, Python, and JS. It should work
The joys of dynamic typing.
You'd be surprised.
But seriously, numbers can be used as booleans in an impressive number of languages. Including machine code for almost every machine out there.
That's the native python version, for those curious
The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.
Eh, reads pretty naturally to me. That said,
(like I lisp)
It's kinda natural to me having used Perl a lot.
At least you guys have ternary syntax cries in kotlin.
I think it's just what you're used to. Imo it really matters that it's keywords and not operator symbols - it's meant to read closer to natural language. I prefer the c version when it's ? and :, but I like them this way round when it's if and else.
That's way too non-convoluted enough
Python is kinda like that in general, unless you try to make it read like ass
Why is the return first?
I think the idea is it reads more naturally, so you can read it like this
return A if statement is true else return B
Edit... I reread your comment and realized that python does it differently and that everything I typed was irrelevant... I'm still gonna leave it if anyone is interested in ternary expressions, but I suppose the answer to your question is, that's just how python does it.
That's how ternary operators are designed to work. In essence, if you're looking to do a single line if/then, you can directly assign a variable from the result of a ternary expression.
As an example, I was scripting something earlier where there may or may not be a value returned from a function, but I still had to do something with that return value later. For this thing, I was using JavaScript.
I ended up with:
If I were to write that out in a typical if/then it would be:
A ternary starts with a boolean expression, then the if true value, else the false value. That's returned to either a variable or if in a function like my example, to the object calling the function. It's just a way to write less code that in many cases is easier to read.
Oh wow, I think I hate that... Condition between the results? Yuck.