个性化阅读
专注于IT技术分析

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

考虑下面的Python程序。

# 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”, 它们与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中的输出:

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

Python 3中的输出:

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

我们可能想尝试以下更有趣的程序:

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

本文作者:阿比·拉西(Abhay Rathi)。如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

赞(1)
未经允许不得转载:srcmini » Python中整数的最大可能值是多少?

评论 抢沙发

评论前必须登录!