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

📅  最后修改于: 2022-05-13 01:54:52.818000             🧑  作者: Mango

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

考虑下面的Python程序。

Python3
# A Python program to demonstrate that we can store
# large numbers in Python
 
x = 10000000000000000000000000000000000000000000
x = x + 1
print (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))


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))


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


输出 :

10000000000000000000000000000000000000000001

在Python中,整数的值不受位数的限制,可以扩展到可用内存的限制(来源: thisthis 。因此,我们永远不需要任何特殊的安排来存储大数(想象一下在 C/C++ 中进行上述算术)。
附带说明一下,在Python 3 中,所有类型的整数只有一种类型“int”。在Python 2.7 中。有两种独立的类型“int”(32 位)和“long int”,与Python 3.x 的“int”相同,即可以存储任意大的数字。

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 中的输出:


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 中的输出:


我们可能想尝试更多有趣的程序,如下所示:

Python3

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