为什么python会在大指数结果的末尾添加一个“L"? [英] Why does python add an 'L' on the end of the result of large exponents?

查看:60
本文介绍了为什么python会在大指数结果的末尾添加一个“L"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您已经注意到,python 会在大指数结果的末尾添加一个 L,如下所示:

<预><代码>>>>25 ** 2588817841970012523233890533447265625L

经过一些测试,我发现任何低于 10 的数字都不包括 L.例如:

<预><代码>>>>9**9387420489

这很奇怪,那么,为什么会发生这种情况,有什么方法可以防止吗?感谢所有帮助!

解决方案

Python 支持任意精度整数,这意味着您可以表示比普通 32 位或 64 位整数类型更大的数字.L 告诉您文字何时属于这种类型而不是常规整数.

注意,L 只出现在解释器的输出中,它只是表示类型.如果您改为打印该结果:

<预><代码>>>>打印(25 ** 25)88817841970012523233890533447265625

L 不会被打印出来.

在 Python 3 中,这些类型已经合并,所以 Python 3 输出:

Python 3.4.0(默认,2014 年 4 月 11 日,13:05:11)[GCC 4.8.2] 在 Linux 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>24 ** 241333735776850284124449081472843776

If you've noticed, python adds an L on to the end of large exponent results like this:

>>> 25 ** 25
88817841970012523233890533447265625L

After doing some tests, I found that any number below 10 doesn't include the L. For example:

>>> 9 ** 9
387420489

This was strange, so, why does this happen, is there any method to prevent it? All help is appreciated!

解决方案

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会在大指数结果的末尾添加一个“L"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆