考虑下面的Python程序。
# A Python program to demonstrate that we can store
# large numbers in Python
x = 10000000000000000000000000000000000000000000;
x = x + 1
print (x)
输出:
10000000000000000000000000000000000000000001
在Python中,整数的值不受位数限制,并且可以扩展到可用内存的限制。因此,我们不需要任何特殊的安排来存储大量数字。
附带说明一下,在Python 3中,所有类型的整数只有一种类型“int”。在Python 2.7中有两种单独的类型“ int”(32位)和“ long int”,与Python 3.x的“ int”相同,即可以存储任意大的数字。
# 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 输出:
Python 3 输出:
我们可能想尝试以下更有趣的程序:
# Printing 100 raise to power 100
print(100**100)