print( ["even", "odd"][num % 2] )
If you need to avoid evaluating the wrong branch:
print( [lambda: "even", lambda: "odd"][num % 2]() )
Cursed
Not as cursed as
print("eovdedn"[n%2::2])
print("odd" if num % 2 else "even")
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)
Why is the return first?
For optimal performance, you should rewrite it in Rust:
inline_python::python! { print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number)) };
And now you can use wasm to run it in a browser!
Please. That’s C’s ternary operator. JS is just a pile of garbage cosplaying as a programming language
Why do you say it’s a pile of garbage?
Because of all the garbage
Clearly the garbage collector is too effective
No they’re not supposed to be piling it up
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.
0 is false in C, Python, and JS. It should work