一、核心知识点
1. 关键概念
- 核心定义:
- 没有小数点的数字,可以是正数、负数,也可以是0;
- 当数据很大时,可以使用_分组;
- 整数的上限值取决于执行代码的计算机的内存和处理能力。
二、代码实现
没有小数点的数字,可以是正数、负数,也可以是0
num1 = 1
num2 = -1
num3 = 0
当数据很大时,可以使用_分组
num1 = 1_000
num2 = 10_000
num3 = 100_000_000_000
整数的上限值取决于执行代码的计算机的内存和处理能力
num1 = 999999999999999999999999999999999999999999999999999
三、踩坑记录与解决方法
1. 整数的上限值取决于执行代码的计算机的内存和处理能力,那为什么执行以下代码会报错?
num = 9 ** 9999
print(num)
ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit
解答:
这是由于python在执行时先将数字转换成字符串,然后再打印。数字在转换成字符串过程中,不能超过4300位。
修改限制sys.set_int_max_str_digits(0)
0 代表没有限制;
这行代码用到了sys模块,所以要在程序开始引入sys模块import sys
import sys
num = 9 ** 99999
sys.set_int_max_str_digits(0)
print(num)