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

That's the native python version, for those curious

[-] DreadPirateShawn 88 points 1 month 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 month ago* (last edited 1 month ago)

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

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

Lisps makes more sense to me though

(if condition a b)

VS

a if condition else b

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

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

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

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

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

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

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

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

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

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

[-] rbos@lemmy.ca 4 points 1 month ago
[-] SpaceNoodle@lemmy.world 14 points 1 month ago
load more comments (8 replies)
[-] l3mming@lemmy.world 3 points 1 month ago* (last edited 1 month 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.

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

That's way too non-convoluted enough

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

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

[-] wise_pancake@lemmy.ca 6 points 1 month 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 month 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 month ago

Clearly an inferior language. /s

load more comments (1 replies)
[-] Ephera@lemmy.ml 118 points 1 month 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 month ago* (last edited 1 month ago)

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

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

Full circle 😆

[-] SpaceNoodle@lemmy.world 61 points 1 month 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 month ago

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

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

Because of all the garbage

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

Clearly the garbage collector is too effective

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

No they're not supposed to be piling it up

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

Is a garbage collector not a garbage disposal. Smh.

load more comments (1 replies)
[-] mdhughes@lemmy.sdf.org 42 points 1 month 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 month ago
[-] gamma@programming.dev 13 points 1 month ago

Not as cursed as

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

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

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

Peak programming

[-] PattyMcB@lemmy.world 10 points 1 month 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 month ago* (last edited 1 month ago)

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

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

I love something = condition and result1 or result2 in lua

[-] Randelung@lemmy.world 10 points 1 month ago* (last edited 1 month ago)
[-] SatyrSack@lemmy.sdf.org 2 points 1 month 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 month ago* (last edited 1 month 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)
[-] kn0wmad1c@programming.dev 2 points 1 month ago* (last edited 1 month 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 month ago

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

[-] kn0wmad1c@programming.dev 3 points 1 month ago
[-] SatyrSack@lemmy.sdf.org 2 points 1 month 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 month 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)
[-] Maven@lemmy.zip 2 points 1 month ago

In JS 0 is the same as False

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

The joys of dynamic typing.

[-] marcos@lemmy.world 2 points 1 month 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
view more: next ›
this post was submitted on 12 Jun 2025
576 points (100.0% liked)

Programmer Humor

25518 readers
2254 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 2 years ago
MODERATORS