Integer
一句话解释
integer (int) 是 整个 数字 不使用 decimal 点, 如 42 or -7.
用大白话说
integer 是整个数字 — 正, 负, or 零 — 不使用 fractional 部分. In Python, int type has arbitrary precision, meaning integers can be as large as memory allows (no overflow).
You create integers by writing them literally (42), or by converting with int(). Integer arithmetic uses +, -, *, // (floor division), % (modulo), and ** (exponentiation).
Python 3 has only one integer type (int). There is no separate long type — all integers are arbitrary precision. Division with / always returns a float, even for integers; use // for integer division.
示例
a = 42
b = -7
c = int("100")
print(a + b) # 35
print(a // 8) # 5 (floor division)
print(a % 8) # 2 (remainder)
print(2 ** 10) # 1024
print(type(a)) # <class 'int'>Integers support arithmetic, floor division, modulo, and exponentiation.
常见混淆
容易混淆的对象: float
关键区别: integer 是 整个 数字 不使用 decimal 点. float 有 decimal 点 and 能 represent fractional 值. Use int() 到 convert float 到 integer (truncating 朝向 零).
相关术语
相关资源
相关课程
- 数据 类型
Integer 类型.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。