Why do integers in database row tuple have an 'L' suffix?
Because in versions of Python before Python 3, long integer literals were indicated with an l
or L
suffix. In Python 3, int
s and long
s 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
Why do integers in database row tuple have an 'L' suffix?
Because in versions of Python before Python 3, long integer literals were indicated with an l
or L
suffix. In Python 3, int
s and long
s 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
Why does python add an 'L' on the end of the result of large exponents?
Python supports arbitrary precision integers, meaning you're able to represent larger numbers than a normal 32 or 64 bit integer type. The L
tells you when a literal is of this type and not a regular integer.
Note, that L
only shows up in the interpreter output, it's just signifying the type. If you print that result instead:
>>> print(25 ** 25)
88817841970012523233890533447265625
The L
doesn't get printed.
In Python 3, these types have been merged, so Python 3 outputs:
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 24 ** 24
1333735776850284124449081472843776
Python : Do not include L at the end of the outcome for : randint(100000000000000000000, 999999999999999999999)
The reason there's an L
there is because this is too large to fit into an int
,* so it's a long
. See Numeric Types — int
, long
, float
, complex
in the docs for more details.
So, why do you get that L
, and how do you get rid of it?
Python has two different ways to turn a value into a string representation:
repr
either returns the canonical source-code representation, or something like<__main__.Eggs at 0x10fbd8908>
).str
returns a human-friendly representation.
For example, for strings, str('abc')
is abc
, while repr('abc')
is 'abc'
.
And for longs, str(1L)
is 1
, while repr(1L)
is 1L
.
When you just type an expression at the interactive prompt, it uses repr
. But when you use the print
command, it uses str
. So, if you want to see the value without the L
, just print
it:
print randint(100000000000000000000, 999999999999999999999)
If you want to, e.g., save the string in a variable or write it to a file, you have to call str
explicitly.
But if you just want to use it as a number, you don't have to worry about this at all; it's a number, and int
and long
values can be intermixed freely (as of Python 2.3 or so).
And if you're trying to store it in a MySQL database, whichever MySQL interface you use won't care whether you're giving it int
values or long
, as long as they fit into the column type.**
Or you could upgrade to Python 3.x, where there is no separate long
type anymore (all integers are int
, no matter how big) and no L
suffix.
* The exact cutoff isn't documented anywhere, but at least for CPython, it's whatever fits into a C long
on your platform. So, on most 64-bit platforms, the max value is (1<<63)-1; on the other 64-bit platforms, and all 32-bit platforms, it's (1<<31)-1. You can see for yourself on your platform by printing sys.maxint
. At any rate, your number takes 70 bits, so unless someone ports Python 2.x to a platform with 128-bit C long
s, it won't fit.
** Note that your values are too big to fit into even a MySQL BIGINT
, so you're going to be using either DECIMAL
or NUMERIC
. Depending on which interface you're using, and how you've set things up, you may have to convert to and from strings manually. But you can do that with the str
and int
functions, without worrying about which values fit into the int
type and which don't.)
Converting all the final letters of numbers (all contained in a list of tuples) in a number
Just proceed graphing, the L stands for long and will not be a problem.
L' stands for count query in PostgreSQL
In Python 2 there was a distinction between integers and long integers. The L means 245 is represented internally as a long integer.
See here for more info.
You should really upgrade to Python 3. For many reasons besides this L.
python list.append(values) adding L to values
Because print
returns human readable format
while if you use repr
in place of print
at your statement
print val
you actually find 'L' appended to every integer
Check here
In [52]: a = 1l
In [53]: print a
1
In [54]: type(a)
Out[54]: long
In [58]: repr(a)
Out[58]: '1L'
In [55]: s = []
In [56]: s.append(a)
In [57]: s
Out[57]: [1L]
because mysql
and other databases stores integer value as long.
How to get the Ids from a django model
values_list()
by default returns the values grouped into tuples.
If you don't want that, use
values_list('id', flat=True)
Related Topics
Why Is Dictionary Ordering Non-Deterministic
How to Log While Using Multiprocessing in Python
How to Detect Whether a Python Variable Is a Function
Is Python Interpreted, or Compiled, or Both
How to Execute Raw SQL in Flask-Sqlalchemy App
Parse a .Py File, Read the Ast, Modify It, Then Write Back the Modified Source Code
How to Open a File for Both Reading and Writing
Performance of Pandas Apply VS Np.Vectorize to Create New Column from Existing Columns
How to Access the Query String in Flask Routes
How to Validate a Url with a Regular Expression in Python
How to Specify New Lines on Python, When Writing on Files
How to Validate Ip Address in Python
How to Run an External Command Asynchronously from Python
Is Shared Readonly Data Copied to Different Processes for Multiprocessing
In Python, How to Read the Exif Data for an Image
In Python, How to Convert a 'Datetime' Object to Seconds