数字类型 #
Python支持三种数字类型:整数(int)、浮点数(float)和复数(complex)。
一、整数(int) #
整数是没有小数部分的数字,可以是正数、负数或零。
1.1 整数的创建 #
python
# 十进制
a = 10
b = -25
c = 0
# 二进制(以0b或0B开头)
binary = 0b1010 # 等于10
print(binary) # 10
# 八进制(以0o或0O开头)
octal = 0o12 # 等于10
print(octal) # 10
# 十六进制(以0x或0X开头)
hex_num = 0xA # 等于10
print(hex_num) # 10
1.2 整数的特点 #
python
# Python 3的整数没有大小限制
big_num = 10 ** 100 # 100位的大数
print(big_num)
# 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
# 类型检查
x = 10
print(type(x)) # <class 'int'>
# isinstance检查
print(isinstance(x, int)) # True
1.3 整数转换 #
python
# 从其他类型转换
int(3.9) # 3(截断,不是四舍五入)
int(-3.9) # -3
int("123") # 123
int("1010", 2) # 10(二进制字符串转十进制)
int("FF", 16) # 255(十六进制字符串转十进制)
# 转换为其他进制字符串
bin(10) # '0b1010'
oct(10) # '0o12'
hex(255) # '0xff'
1.4 位运算 #
python
a = 60 # 二进制:0011 1100
b = 13 # 二进制:0000 1101
# 按位与(&)
print(a & b) # 12 (0000 1100)
# 按位或(|)
print(a | b) # 61 (0011 1101)
# 按位异或(^)
print(a ^ b) # 49 (0011 0001)
# 按位取反(~)
print(~a) # -61
# 左移(<<)
print(a << 2) # 240 (1111 0000)
# 右移(>>)
print(a >> 2) # 15 (0000 1111)
二、浮点数(float) #
浮点数是有小数部分的数字。
2.1 浮点数的创建 #
python
# 普通写法
a = 3.14
b = -0.5
c = 0.0
# 科学计数法
d = 1.5e2 # 150.0
e = 2.5e-3 # 0.0025
f = 1e10 # 10000000000.0
# 下划线分隔(Python 3.6+)
g = 1_000.5 # 1000.5
2.2 浮点数精度 #
python
# 浮点数精度问题
print(0.1 + 0.2) # 0.30000000000000004(不是精确的0.3)
# 使用decimal处理精确计算
from decimal import Decimal
a = Decimal('0.1')
b = Decimal('0.2')
print(a + b) # 0.3(精确结果)
# 设置精度
from decimal import getcontext
getcontext().prec = 4
print(Decimal('1') / Decimal('3')) # 0.3333
2.3 浮点数转换 #
python
float(10) # 10.0
float("3.14") # 3.14
float("1e3") # 1000.0
float("inf") # inf(无穷大)
float("-inf") # -inf(负无穷大)
float("nan") # nan(非数字)
2.4 特殊值 #
python
# 无穷大
positive_inf = float('inf')
negative_inf = float('-inf')
print(positive_inf > 1e308) # True
print(positive_inf + 1) # inf
# 非数字(NaN)
nan_value = float('nan')
print(nan_value == nan_value) # False(NaN不等于任何值)
import math
print(math.isnan(nan_value)) # True
三、复数(complex) #
复数由实部和虚部组成,Python原生支持复数运算。
3.1 复数的创建 #
python
# 使用j或J表示虚部
c1 = 3 + 4j
c2 = 2j
c3 = complex(3, 4) # 3 + 4j
c4 = complex('3+4j') # 3 + 4j
print(c1) # (3+4j)
3.2 复数的属性和方法 #
python
c = 3 + 4j
# 获取实部和虚部
print(c.real) # 3.0
print(c.imag) # 4.0
# 获取共轭复数
print(c.conjugate()) # (3-4j)
# 计算模(绝对值)
print(abs(c)) # 5.0(sqrt(3^2 + 4^2))
3.3 复数运算 #
python
a = 3 + 4j
b = 1 + 2j
# 加法
print(a + b) # (4+6j)
# 减法
print(a - b) # (2+2j)
# 乘法
print(a * b) # (-5+10j)
# 除法
print(a / b) # (2.2+0.4j)
# 幂运算
print(a ** 2) # (-7+24j)
四、数字运算 #
4.1 基本算术运算 #
python
a, b = 10, 3
# 加法
print(a + b) # 13
# 减法
print(a - b) # 7
# 乘法
print(a * b) # 30
# 除法(结果是浮点数)
print(a / b) # 3.3333333333333335
# 整除(向下取整)
print(a // b) # 3
# 取余
print(a % b) # 1
# 幂运算
print(a ** b) # 1000
print(pow(a, b)) # 1000
4.2 整除的特殊情况 #
python
# 整除向负无穷方向取整
print(10 // 3) # 3
print(-10 // 3) # -4(不是-3)
print(10 // -3) # -4
# 取余与整除的关系
# a = (a // b) * b + a % b
print(-10 % 3) # 2
4.3 优先级 #
python
# 优先级:** > * / // % > + -
result = 2 + 3 * 4 ** 2
# 等价于:2 + (3 * (4 ** 2)) = 2 + (3 * 16) = 50
print(result) # 50
# 幂运算是右结合的
print(2 ** 3 ** 2) # 512(等价于 2 ** (3 ** 2))
五、数学函数 #
5.1 内置函数 #
python
# 绝对值
abs(-5) # 5
abs(-3.14) # 3.14
abs(3 + 4j) # 5.0(复数的模)
# 最大值和最小值
max(1, 5, 3, 2) # 5
min(1, 5, 3, 2) # 1
max([1, 2, 3]) # 3
min([1, 2, 3]) # 1
# 四舍五入
round(3.14159) # 3
round(3.14159, 2) # 3.14
round(3.5) # 4(银行家舍入法)
round(2.5) # 2(银行家舍入法)
# 幂运算
pow(2, 3) # 8
pow(2, 3, 5) # 3((2 ** 3) % 5)
# 求和
sum([1, 2, 3, 4, 5]) # 15
5.2 math模块 #
python
import math
# 常量
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.inf) # inf
print(math.nan) # nan
# 向上取整和向下取整
print(math.ceil(3.2)) # 4
print(math.floor(3.8)) # 3
# 平方根
print(math.sqrt(16)) # 4.0
# 对数
print(math.log(10)) # 2.302585092994046(自然对数)
print(math.log10(100)) # 2.0(以10为底的对数)
print(math.log2(8)) # 3.0(以2为底的对数)
# 三角函数(参数为弧度)
print(math.sin(math.pi / 2)) # 1.0
print(math.cos(0)) # 1.0
print(math.tan(math.pi / 4)) # 0.9999999999999999
# 角度与弧度转换
print(math.degrees(math.pi)) # 180.0
print(math.radians(180)) # 3.141592653589793
# 判断函数
print(math.isnan(float('nan'))) # True
print(math.isinf(float('inf'))) # True
print(math.isfinite(100)) # True
5.3 random模块 #
python
import random
# 随机整数
print(random.randint(1, 10)) # 1到10之间的随机整数(包含两端)
# 随机浮点数
print(random.random()) # 0.0到1.0之间的随机浮点数
print(random.uniform(1, 10)) # 1到10之间的随机浮点数
# 随机选择
items = ['苹果', '香蕉', '橙子']
print(random.choice(items)) # 随机选择一个元素
# 随机抽样
print(random.sample(items, 2)) # 随机选择2个元素(不重复)
# 打乱顺序
cards = [1, 2, 3, 4, 5]
random.shuffle(cards)
print(cards) # 顺序被打乱
# 设置随机种子(用于重现结果)
random.seed(42)
print(random.random()) # 每次运行都是相同的随机数
六、类型判断与转换 #
6.1 类型判断 #
python
x = 10
y = 3.14
z = 1 + 2j
# 使用type()
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'complex'>
# 使用isinstance()(推荐)
print(isinstance(x, int)) # True
print(isinstance(y, float)) # True
print(isinstance(z, complex)) # True
# 检查是否为数字类型
print(isinstance(x, (int, float, complex))) # True
6.2 数值转换 #
python
# 整数转浮点数
float(10) # 10.0
# 浮点数转整数
int(3.9) # 3(截断)
int(-3.9) # -3
# 四舍五入转整数
round(3.9) # 4
# 字符串转数字
int("123") # 123
float("3.14") # 3.14
七、数字格式化 #
7.1 格式化输出 #
python
pi = 3.14159265359
# 使用f-string
print(f"{pi:.2f}") # 3.14
print(f"{pi:.4f}") # 3.1416
print(f"{pi:8.2f}") # 3.14(总宽度8,右对齐)
print(f"{pi:<8.2f}") # 3.14 (左对齐)
print(f"{pi:^8.2f}") # 3.14 (居中)
# 科学计数法
print(f"{1000000:.2e}") # 1.00e+06
# 百分比
ratio = 0.756
print(f"{ratio:.2%}") # 75.60%
# 千位分隔符
big_num = 1234567890
print(f"{big_num:,}") # 1,234,567,890
print(f"{big_num:_}") # 1_234_567_890
7.2 format方法 #
python
pi = 3.14159
print("{:.2f}".format(pi)) # 3.14
print("{:+.2f}".format(pi)) # +3.14(显示正号)
print("{:.2%}".format(0.756)) # 75.60%
八、常见陷阱 #
8.1 浮点数精度 #
python
# 精度问题
print(0.1 + 0.2 == 0.3) # False
print(0.1 + 0.2) # 0.30000000000000004
# 解决方案:使用容差比较
def almost_equal(a, b, tolerance=1e-9):
return abs(a - b) < tolerance
print(almost_equal(0.1 + 0.2, 0.3)) # True
# 或者使用decimal
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2') == Decimal('0.3')) # True
8.2 整除与取余 #
python
# 负数的整除和取余
print(-7 // 3) # -3(向负无穷取整)
print(-7 % 3) # 2
# 理解:-7 = (-3) * 3 + 2
8.3 is与== #
python
# 小整数缓存
a = 256
b = 256
print(a is b) # True
c = 257
d = 257
print(c is d) # False(超过缓存范围)
# 比较值应该使用==
print(c == d) # True
九、总结 #
| 类型 | 描述 | 示例 |
|---|---|---|
| int | 整数,无大小限制 | 10, -5, 0b1010 |
| float | 浮点数 | 3.14, 1.5e2 |
| complex | 复数 | 3+4j |
| 运算 | 符号 | 示例 |
|---|---|---|
| 加法 | + | 10 + 3 = 13 |
| 减法 | - | 10 - 3 = 7 |
| 乘法 | * | 10 * 3 = 30 |
| 除法 | / | 10 / 3 = 3.333… |
| 整除 | // | 10 // 3 = 3 |
| 取余 | % | 10 % 3 = 1 |
| 幂运算 | ** | 10 ** 3 = 1000 |
最后更新:2026-03-16