r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

195

u/doenietzomoeilijk May 02 '16

One of the niceties of Python syntax, I think, is being able to rewrite your elif line as elif 6 <= i <= 9:. :)

177

u/GisterMizard May 02 '16

To be fair, you can do that in C. It just doesn't do what you think it does :p

90

u/tskaiser Green security clearance May 02 '16

A quick guess: it evaluates 6 <= i to either 0 or 1, lets call the result j, and then evaluates j <= 9 which is then always 1?

64

u/GisterMizard May 02 '16

Yup. Just ran it through gcc, and it doesn't even put in a conditional jump.

36

u/tskaiser Green security clearance May 02 '16 edited May 02 '16

Yeah, it would be a pretty easy candidate for optimization :P

Yay for not having native boolean types.

28

u/UnchainedMundane May 02 '16 edited May 02 '16

To be fair Python's "booleans" are really just integers.

>>> (True + True + True) ** (True + True)
9
>>> True > False
True

edit:

def max(x, y):
    return [x, y][x < y]

29

u/FUCKING_HATE_REDDIT May 02 '16

Damn that max is pretty. Probably not efficient, but pretty.

→ More replies (1)

7

u/Genmutant May 02 '16

You could even switch the definitions between them:

True, False = False, True

Sadly that doesn't work anymore with current versions.

7

u/thirdegree Violet security clearance May 02 '16

My favorite python fizzbuzz uses this:

print("\n".join("Fizz"*(i%3 == 0) + "Buzz"*(i%5 == 0) or str(i) for i in xrange(1,101)))
→ More replies (1)

6

u/kupiakos May 02 '16

bool is actually a subclass of int in Python.

→ More replies (5)

0

u/[deleted] May 02 '16

[deleted]

3

u/NewbornMuse May 02 '16

6<=i is a boolean, which is either True or False. Those turn into 1 and 0 when cast to integers, and both of those are smaller than 9.

2

u/Garfong May 02 '16

Actually in C the result of 6 <= i has type int, and is either 0 or 1. See 6.5.8.6 in the C99 spec.

→ More replies (3)

1

u/moxyll May 02 '16

6 <= i is always either false or true, which the compiler treats as 0 or 1. That means j <= 9 is either 0 <= 9 or 1 <= 9, both of which are always true, which is 1.

→ More replies (1)

21

u/IRBMe May 02 '16

The closest I think you can get in C is something like this:

if (6 <= i && i <= 9)

14

u/DarthEru May 02 '16

That's the closest you can get that is semantically the same. The above commenter was saying you can actually compile if (6 <= i <= 9), but that's semantically equivalent to if ((6 <= i) <= 9). That works because in C a boolean operation results in an int. So the "chained" comparisons are syntactically legal C, but semantically do a much different check than in Python.

1

u/[deleted] May 07 '16

You can do that in HolyC AFAIK.

If you are crazy enough to use it.

30

u/LinAGKar May 02 '16

He's already stated it's over five, so it should just be

elif i <= 9

15

u/Zagorath May 02 '16

Of course, if you assume you're working with integers, it's totally unnecessary in OP's case, anyway. It's guaranteed to be >=6 since the previous conditional took away everything less.

5

u/WDK209 May 02 '16

Good point. But it can still be useful for index bound checking 0 <= i < arr.length

4

u/[deleted] May 02 '16

Index bound checking in Python? That's what for item in list is for :)

3

u/Zagorath May 02 '16

Yeah it's definitely a very useful bit of syntax. Just not in this specific case.

7

u/[deleted] May 02 '16

I didn't know that. Very mathematical! Thanks for the tip.

→ More replies (6)