pythonpython-2.x

What does a trailing 'L' (suffix) mean on an integer value in Python 2?


I wrote some code to query a row from an SQL database, zip the values with column names, and thus product this dictionary:

{'estimated': '', 
 'suffix': '', 
 'typeofread': 'g', 
  'acct_no': 901001000L, 
  'counter': 0, 
  'time_billed': datetime.datetime(2012, 5, 1, 9, 5, 33), 
  'date_read': datetime.datetime(2012, 3, 13, 23, 19, 45), 
  'reading': 3018L, 
  'meter_num': '26174200'}

Notice for example that the value for 'reading' is written as 3018L, not just 3018. Why?

I found that I can remove the L by converting the value with int, but I want to understand what it means.


Solution

  • Because in versions of Python before Python 3, long integer literals were indicated with an l or L suffix. In Python 3, ints and longs have been merged into just int, which functions pretty much like long used to.

    Do note that, technically, Python( 2)'s int was equivalent to C's long, while Python's long was more like a BigNumber-type thing with unlimited precision (which is now the case for Python 3's int type.)

    http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex