Python中整数的最大可能值是多少?

考虑下面的Python程序。

null

Python3

# A Python program to demonstrate that we can store
# large numbers in Python
x = 10000000000000000000000000000000000000000000
x = x + 1
print (x)


输出:

10000000000000000000000000000000000000000001

在Python中,整数的值不受位数的限制,可以扩展到可用内存的极限(来源: ) 因此,我们不需要任何特殊的安排来存储大量的数字(想象一下在C/C++中进行上述运算)。 作为补充说明,在Python 3中,所有类型的整数只有一种类型“int”。在Python 2.7中。有两种不同的类型“int”(32位)和“long int”,与Python3的“int”相同。x、 也就是说,可以存储任意大的数字。

python

# A Python program to show that there are two types in
# Python 2.7 : int and long int
# And in Python 3 there is only one type : int
x = 10
print ( type (x))
x = 10000000000000000000000000000000000000000000
print ( type (x))


Python 2.7中的输出:

<type 'int'><type 'long'>

Python3

# A Python3 program to show that there are two types in
# Python 2.7 : int and long int
# And in Python 3 there is only one type : int
x = 10
print ( type (x))
x = 10000000000000000000000000000000000000000000
print ( type (x))


Python 3中的输出:

<type 'int'><type 'int'>

我们可能想尝试以下更有趣的节目:

Python3

# Printing 100 raise to power 100
print ( 100 * * 100 )


本文由 阿比拉蒂 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享