跳转至

整型类型

整型(int) 指没有小数点的数字,可为正数、负数,也可以为 0。

# 整型类型,指没有小数点的数字,可为正负数,也可以为0

age = 25  # 整数
print(age)
age = -30  # 负整数
print(age)
age = 0  # 零
print(age)


# 表达大数字,可以通过下划线来分隔数字,使代码更易读
salary = 300_000
house_proce = 1_000_000_000
print(salary)
print(house_proce)

# 扩展内容
# 1. 在 Python 中,整数上限值由执行代码的计算机的内存和处理能力
a = 999999999999999999999999999999999999 
print(a)
a = 9 ** 9999  # a ** b 表示 a 的 b 次方
b = a + 100 # 此可以证明并没有超过 int 的最大值
# 解决方法
import sys
sys.set_int_max_str_digits(0)  # 设置最大字符串长度为 不限制 位
print(a) # 报错位置
"""
ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() 
to increase the limit

看似是超过最大上限,其实是由于在 print 时,Python 自动将整数转换为字符串,超过了最大字符串长度(一般为 4300 位)。
"""