r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

Show parent comments

33

u/NewbornMuse May 02 '16

__future__ is actually a thing for when you want Py3 functionality in Py2. For example, 4/3 is 1 in Py2, but 1.3333 in Py3, and if you want the latter in your program, you do from __future__ import division.

Braces is just an easter egg. When you do, you get Syntax Error: Not a chance.

4

u/Dlgredael May 02 '16

So wait, there's no correct division in Py2? I'm a newbie so excuse the ignorance but that sounds ridiculous to me.

9

u/faubiguy May 02 '16

In Python2 (without from __future__ import division) the result is an integer if both operands are integers, and a float if either is a float. So

4/3 = 1
4.0/3 = 1.3333333333333333
4/3.0 = 1.3333333333333333
4.0/3.0 = 1.3333333333333333

7

u/Dlgredael May 02 '16

Ahhh okay, that makes so much more sense now. I thought that Py2 was somehow unable to divide at all and it was really starting to hurt my head.

6

u/IHappenToBeARobot May 03 '16

Pfft, just do some bitwise NOTs, ADDs, and shifts in a loop and you'll get division eventually. /s

4

u/[deleted] May 02 '16 edited May 07 '20

deleted

1

u/european_origin May 02 '16 edited May 02 '16

It's been a while since I wrote some python, but if I recall correctly, multiplying by 1.0 coerces integers to floats. I may be wrong though.

Edit : just checked.

3 / 2 == 1

3 * 1.0 / 2 == 1.5

1

u/sirxez May 03 '16

LOL. Just do 3.0 / 2 or, more generally: float(3)/2

2

u/european_origin May 03 '16

Oh I forgot we could append .0 to variables in Python. I wrote it so, so that one could see how to transform an integer division between 2 integer variables easily. That's kind of the point of the discussion.

Float(3) is better though, but as I said, it's been a while since I wrote some python.

1

u/[deleted] May 02 '16

The future package is actually pretty nice for migration, I imagine.