578
top 50 comments
sorted by: hot top controversial new old
[-] entropicdrift@lemmy.sdf.org 138 points 1 year ago
print("odd" if num % 2 else "even")

That's the native python version, for those curious

[-] DreadPirateShawn 88 points 1 year ago

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.

[-] balsoft@lemmy.ml 22 points 1 year ago* (last edited 1 year ago)

Eh, reads pretty naturally to me. That said, (like I lisp)

[-] Shareni@programming.dev 17 points 1 year ago

Lisps makes more sense to me though

(if condition a b)

VS

a if condition else b

[-] balsoft@lemmy.ml 9 points 1 year ago

I was more talking about (+ a b) and such.

[-] rovingnothing29@lemmy.world 2 points 1 year ago

Oh, (you) (really) (like) (Lisp)? (That's) (great!)

[-] hydroptic@sopuli.xyz 5 points 1 year ago* (last edited 1 year ago)

(is great (oh (really (like-p lisp you))))

[-] rbos@lemmy.ca 12 points 1 year ago

It's kinda natural to me having used Perl a lot.

[-] SpaceNoodle@lemmy.world 22 points 1 year ago

That's not quite the argument you might think it is

[-] l3mming@lemmy.world 3 points 1 year ago* (last edited 1 year ago)

You clearly haven't used Perl a lot. Perl's ternary looks like:

$even = $num % 2 ? "nay" : "yay";

Incidentally, it is also the same as PHP's, but mainly because PHP stole it.

[-] psud@aussie.zone 2 points 1 year ago

You do get the if in the middle of stuff though in the form print(debug message) if $debug

[-] palordrolap@fedia.io 5 points 1 year ago

Wait until you learn that postfix conditionals are syntactic sugar and the compiler* turns that line into the equivalent of $debug and print(debug message), putting the conditional in first place, a lot like the ternary operator.

* Perl compiles to bytecode before running.

The ternary operator itself isn't implemented in terms of and (and or) but it could be.

[-] psud@aussie.zone 4 points 1 year ago

Luckily I don't need to read or write bytecode and all that matters to me is the syntax

load more comments (1 replies)
load more comments (1 replies)
[-] Gork@sopuli.xyz 8 points 1 year ago

That's way too non-convoluted enough

[-] entropicdrift@lemmy.sdf.org 17 points 1 year ago

Python is kinda like that in general, unless you try to make it read like ass

[-] wise_pancake@lemmy.ca 6 points 1 year ago

You would not believe the number of people I’ve interviewed who excel at making Python read like ass.

[-] entropicdrift@lemmy.sdf.org 6 points 1 year ago

I mean, it does have enough ways to write the same thing that it can really allow for some funny code golf, but some people just have no sense of readability whatsoever.

[-] Colloidal@programming.dev 5 points 1 year ago

Clearly an inferior language. /s

[-] laurelraven@lemmy.zip 2 points 1 year ago

Oh wow, I think I hate that... Condition between the results? Yuck.

[-] Ephera@lemmy.ml 119 points 1 year ago

For optimal performance, you should rewrite it in Rust:

inline_python::python! {
    print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number))
};
[-] ATPA9@feddit.org 68 points 1 year ago* (last edited 1 year ago)

And now you can use wasm to run it in a browser!

[-] victorz@lemmy.world 21 points 1 year ago

Full circle 😆

[-] SpaceNoodle@lemmy.world 61 points 1 year ago

Please. That's C's ternary operator. JS is just a pile of garbage cosplaying as a programming language

[-] victorz@lemmy.world 4 points 1 year ago

Why do you say it's a pile of garbage?

[-] SpaceNoodle@lemmy.world 21 points 1 year ago

Because of all the garbage

[-] Fillicia@sh.itjust.works 8 points 1 year ago

Clearly the garbage collector is too effective

[-] filcuk@lemmy.zip 5 points 1 year ago

No they're not supposed to be piling it up

[-] piccolo@sh.itjust.works 8 points 1 year ago

Is a garbage collector not a garbage disposal. Smh.

load more comments (1 replies)
[-] mdhughes@lemmy.sdf.org 42 points 1 year ago

print( ["even", "odd"][num % 2] )

If you need to avoid evaluating the wrong branch:

print( [lambda: "even", lambda: "odd"][num % 2]() )

[-] FireIced@lemmy.super.ynh.fr 9 points 1 year ago
[-] gamma@programming.dev 13 points 1 year ago

Not as cursed as

print("eovdedn"[n%2::2]) 
load more comments (1 replies)
[-] owl@infosec.pub 16 points 1 year ago

Just send pseudo code to AI and compile straight to binary.

[-] scrubbles@poptalk.scrubbles.tech 13 points 1 year ago

Peak programming

[-] capuccino@lemmy.world 10 points 1 year ago

I love something = condition and result1 or result2 in lua

[-] Randelung@lemmy.world 10 points 1 year ago* (last edited 1 year ago)
[-] SatyrSack@lemmy.sdf.org 2 points 1 year ago

Are you just referring to how Python uses the English and/or instead of the more common &&/||? I think what the user above you was talking about was Lua's strange ternary syntax using and/or.

[-] lime@feddit.nu 6 points 1 year ago* (last edited 1 year ago)

no, the linked table shows how python also returns the first non-falsey result of an a or b expression rather than just giving a boolean. it's useful for initialising optional reference args:

def foo(a: list = None)
    a = a or []

works with and as well.

load more comments (1 replies)
[-] PattyMcB@lemmy.world 10 points 1 year ago

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

[-] conditional_soup@lemm.ee 6 points 1 year ago* (last edited 1 year ago)

Are you sure it isn't just that he's Dutch?

[-] kn0wmad1c@programming.dev 2 points 1 year ago* (last edited 1 year ago)

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.

[-] moomoomoo309@programming.dev 8 points 1 year ago

All of those languages will convert numbers into booleans, 0 is false, all other numbers are true.

[-] kn0wmad1c@programming.dev 3 points 1 year ago
[-] SatyrSack@lemmy.sdf.org 2 points 1 year ago

It doesn't make sense. I understand it, but it doesn't make sense.

load more comments (1 replies)
load more comments (2 replies)
[-] sleeplessone@lemmy.ml 5 points 1 year ago

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.

load more comments (1 replies)
[-] Colloidal@programming.dev 2 points 1 year ago

The joys of dynamic typing.

[-] marcos@lemmy.world 2 points 1 year ago

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.

load more comments (3 replies)
load more comments
view more: next ›
this post was submitted on 12 Jun 2025
578 points (100.0% liked)

Programmer Humor

31829 readers
470 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 3 years ago
MODERATORS