i see that // (floordiv) is used in 2.7.12. for lots of things the documentation says in which version it was first used or defined. i don't see it for //. so i am curious if // was in Python all along or if it was added at some point, and if the latter, at which version it was added.
i see that for the numbers i picked, in version 2.7.12, both / and // always give me the same result. does this mean // was added to 2.7.12 to make the transition to Python3 easier?
PEP-238 Implementation
python 2.2
(Oct-09-2018, 07:11 PM)Skaperen Wrote: [ -> ]in version 2.7.12, both / and // always give me the same result
that is expected as / in python2 is floor division (i.e. same as //) for int
EDIT: added
for int to make it clear. tnx, nilamo
They're not the same in python2 for floats, only for ints. In python 2, int / int = int, but in python 3, they wanted to make sure information wasn't lost (and lots of people were confused about why the decimal wasn't in the results).
(Oct-09-2018, 08:09 PM)buran Wrote: [ -> ]that is expected as / in python2 is floor division (i.e. same as //) for int
EDIT: added for int to make it clear. tnx, nilamo
since 2.7.12 supported // and since i believed it was intended to be for transitioning to Python3 i believed / would be the same as in python3. i believed so strongly that i didn't even test it before wondering how it might affect what i was coding today and wonder how older python2 would have this effect.
i had coded a test for version 2 and earlier to use / else use // to get the floor division behavior. now i know that code will be fine on all python2 and python3.
this code is not yet tested:
def intsize(a):
try:
n = int(a,0)
except:
n = int(a)
if bytes == str:
return (len(hex(n))-1)/2
else:
return (len(hex(n))-1)//2