Python sum() returns negative value because the sum is too large for 32bit integer
Question: 
    x = [1, 2, 3, ... ]
    y = sum(x)
    

The sum of `x` is 2165496761, which is larger than the limit of 32bit integer
So `sum(x)` returns -2129470535.

How can I get the correct value by converting it to long integer?

Here is my import list:

    
    
    import math, csv, sys, re, time, datetime, pickle, os, gzip
    from numpy import *
    


Answer: Twenty quatloos says you're using `numpy`'s sum function:

    
    
    >>> sum(xrange(10**7))
    49999995000000L
    >>> from numpy import sum
    >>> sum(xrange(10**7))
    -2014260032
    

So I'd bet you did `from numpy import *` or are using some interface which
does the equivalent.

To verify this, try

    
    
    print type(sum(x))
    

On the example posted elsewhere in this thread:

    
    
    >>> sum([721832253, 721832254, 721832254])
    -2129470535
    >>> type(sum([721832253, 721832254, 721832254]))
    <type 'numpy.int32'>
    

Edit: somebody owes me twenty quatloos! Either don't use the star import
(best), manually set the `dtype`:

    
    
    >>> sum([721832253, 721832254, 721832254],dtype=object)
    2165496761L
    

or refer to the builtin `sum` explicitly (possibly giving it a more convenient
binding):

    
    
    >>> __builtins__.sum([721832253, 721832254, 721832254])
    2165496761L
    

