How to avoid the L in Python

こ雲淡風輕ζ 提交于 2019-12-20 01:14:12

问题


>>> sum(range(49999951,50000000))
  2449998775L

Is there any possible way to avoid the L at the end of number?


回答1:


The L is just for you. (So you know its a long) And it is nothing to worry about.

>>> a = sum(range(49999951,50000000))
>>> a
2449998775L
>>> print a
2449998775

As you can see, the printed value (actual value) does not have the L it is only the repr (representation) that displays the L

Consult this post




回答2:


You are looking at a Python literal representation of the number, which just indicates that it is a python long integer. This is normal. You do not need to worry about that L.

If you need to print such a number, the L will not normally be there.

What happens is that the Python interpreter prints the result of repr() on all return values of expressions, unless they return None, to show you what the expression did. Use print if you want to see the string result instead:

>>> sum(range(49999951,50000000))
2449998775L
>>> print sum(range(49999951,50000000))
2449998775


来源:https://stackoverflow.com/questions/17473195/how-to-avoid-the-l-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!