代码点读机 / Python / 数字

数字 Python · 数字

数字是 Python 中的数值类型,包括整数(`int`)、浮点数(`float`)、复数(`complex`)。整数没有小数部分,浮点数有小数点,复数有实部和虚部。它们用于数学计算和逻辑判断。

怎么用

整数 = 42
浮点数 = 3.14
复数 = 2 + 3j

小例子

print(10 + 5)          # 15
print(3.5 * 2)         # 7.0
print(2 + 3j)          # (2+3j)
print(type(10))        # <class 'int'>
print(type(3.14))      # <class 'float'>
print(type(2+3j))      # <class 'complex'>
**运行结果**:如上。

运行结果

<class 'int'>
<class 'float'>

常见使用场景

- 数学运算。
- 计数、索引。
- 科学计算。

提醒:- 整数可以任意大,不受位数限制。 - 浮点数有精度限制,可能出现舍入误差。 - 复数一般用于科学和工程计算。

官方文档:https://docs.python.org/zh-cn/3/reference/lexical_analysis.html#numeric-literals

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条