[Tutor] More type() puzzlement

Dick Moores rdm at rcblue.com
Sat Oct 27 17:06:02 CEST 2007

Win XP, Python 2.5.1

#!/usr/bin/env python

n = 10000000000 # 10 billion
print "type of 10 billion is", type(n)
n = 1000000000 # 1 billion
print "type of 1 billion is", type(n)

raw_input("press enter to continue")

n = 10000000000
print type(n)
while True:
     if type(n) == long:
         n -= 1000000
         print n, type(n)
print n
print type(n), 'HERE'

As an exercise in using type() I was thinking I could use it to begin 
to find where (without looking it up) a long becomes an int. I show 
that the boundary is somewhere between 10 billion and 1 billion. But 
the above script never ends--never gets to 'HERE'.

Here's part of the output:

6000000 <type 'long'>
5000000 <type 'long'>
4000000 <type 'long'>
3000000 <type 'long'>
2000000 <type 'long'>
1000000 <type 'long'>
0 <type 'long'>
-1000000 <type 'long'>
-2000000 <type 'long'>
-3000000 <type 'long'>
-4000000 <type 'long'>
-5000000 <type 'long'>
-6000000 <type 'long'>

Note that it shows even 1 million and 0 to be longs, whereas
 >>> type(1000000)
<type 'int'>
 >>> type(0)
<type 'int'>

What's going on?


Dick Moores

More information about the Tutor mailing list