September 14th, 2012, 05:12 AM
IndentationError: unexpected indent
>>> for i in range(0,128):
... x = 1 << i
... print "%03d: 0x%X" % (i, x)
and eventually the last print show this ...
I am used to 32 and 64 bit integers, but here Python seems to be happily dealing with 128 bit integers. I was completely supprised that this works, it mus mean Python is storing values in some very wierd way. Can someone tell me whats going on here and how this works
September 14th, 2012, 05:44 AM
Integers in Python are theoretically unbounded (though of course, they are actually bounded by the memory available on the machine). Technically, there are separate 'int' and 'long' types, but the distinction is essentially transparent, and you usually don't need to be aware of it.
September 14th, 2012, 06:02 AM
Thanks, yes I have a "C" mind set. I am writing code to test hardware that is 96 bits wide. Python is great being able to read 3 32 bit values and lets me do ...
bits = ((x<<64) | (y <<32) | z)
I was blown away when that worked. Great language
September 15th, 2012, 02:10 AM
Python is famously one of the most programmer-friendly (and machine-unfriendly) programming languages out there. I see it in a way as a reaction to the crap that a lot of other languages make you put up with, though I don't know if that's any sort of official rationale. (I heard Guido van Rossum speak on Python a few years ago, and as I recall, he didn't spend time bashing other languages.)
Originally Posted by julianh
September 19th, 2012, 03:50 AM
Interesting man/machine trade off you mention. Do all man friendly languages need to be difficult for a computer to execute ? On the one hand after many years of "C" programming my way of thinking has adapted and it still feels easier for me than Python which is unfamiliar. I am also reminded of the time I spent programming in Lisp on Symbolics hardware designed to execute it directly. Lisp is a pretty awsome language if you are into expert systems.
September 19th, 2012, 05:12 AM
Until about version 2.2 or so, python used to distinguish between int and long (bignum) types:
In older versions of python, it was possible to overflow x because it was an integer type and it would throw an OverflowError exception. y is a long/bignum variable and could be incremented as long as there's enough memory in the machine.
In newer versions of python (2.2 and above), the conversion happens automatically, so you can't overflow an int anymore. If the value can't be represented as an int, it will automatically promote it to a long type. So there's no need to add an 'L' at the end to initialize a variable as a long type any more.
The modification was discussed here:
Last edited by Scorpions4ever; September 19th, 2012 at 05:21 AM.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo