以下是Python中常用的函数分类整理,涵盖基础操作、数据处理、文件操作、面向对象等场景,并附上示例说明:
---
### **一、基础内置函数**
| 函数 | 作用 | 示例 |
|----------------------|-----------------------------|-------------------------------|
| `print()` | 输出内容 | `print("Hello")` |
| `len()` | 返回对象长度 | `len([1,2,3])` → 3 |
| `type()` | 返回对象类型 | `type(10)` → `<class 'int'>` |
| `range()` | 生成整数序列 | `list(range(5))` → `[0,1,2,3,4]` |
| `input()` | 获取用户输入 | `name = input("Name: ")` |
| `sum()` | 求和 | `sum([1,2,3])` → 6 |
| `max()/min()` | 返回最大/最小值 | `max([5,2,8])` → 8 |
| `abs()` | 绝对值 | `abs(-3.14)` → 3.14 |
| `round()` | 四舍五入 | `round(3.14159, 2)` → 3.14 |
---
### **二、数据类型转换**
| 函数 | 说明 | 示例 |
|----------------------|-----------------------------|-------------------------------|
| `int()` | 转整数 | `int("10")` → 10 |
| `float()` | 转浮点数 | `float("3.14")` → 3.14 |
| `str()` | 转字符串 | `str(100)` → `"100"` |
| `list()` | 转列表 | `list("abc")` → `['a','b','c']` |
| `tuple()` | 转元组 | `tuple([1,2])` → `(1,2)` |
| `dict()` | 创建字典 | `dict(a=1, b=2)` → `{'a':1, 'b':2}` |
| `set()` | 转集合(去重) | `set([1,1,2])` → `{1,2}` |
---
### **三、字符串处理**
| 函数/方法 | 说明 | 示例 |
|----------------------|-----------------------------|-------------------------------|
| `split()` | 按分隔符拆分字符串 | `"a,b,c".split(",")` → `['a','b','c']` |
| `join()` | 连接字符串 | `"-".join(['a','b'])` → `"a-b"` |
| `strip()` | 去除首尾空格/指定字符 | `" hello ".strip()` → `"hello"` |
| `replace()` | 替换子串 | `"abc".replace("b","x")` → `"axc"` |
| `find()` | 查找子串位置(未找到返回-1) | `"python".find("th")` → 2 |
| `format()` | 格式化字符串 | `"{} {}".format("Hi",5)` → `"Hi 5"` |
| `f-string` (Python 3.6+) | 更简洁的格式化 | `f"{name} is {age} years old"` |
---
### **四、列表/字典操作**
| 函数/方法 | 说明 | 示例 |
|----------------------|-----------------------------|-------------------------------|
| `append()` | 向列表追加元素 | `lst = [1]; lst.append(2)` → `[1,2]` |
| `extend()` | 合并列表 | `[1].extend([2,3])` → `[1,2,3]` |
| `sort()` | 列表排序 | `[3,1,2].sort()` → `[1,2,3]` |
| `sorted()` | 返回新排序列表(不改变原列表)| `sorted([3,1,2])` → `[1,2,3]` |
| `keys()/values()` | 获取字典键/值 | `dict(a=1).keys()` → `['a']` |
| `items()` | 获取键值对(迭代用) | `dict(a=1).items()` → `[('a',1)]` |
| `get()` | 安全获取字典值 | `d.get('x', default=0)` |
---
### **五、文件与系统操作**
| 函数 | 说明 | 示例 |
|----------------------|-----------------------------|-------------------------------|
| `open()` | 打开文件 | `with open('file.txt') as f: ...` |
| `os.listdir()` | 列出目录文件 | `os.listdir('.')` |
| `os.path.join()` | 跨平台路径拼接 | `os.path.join('dir','file.txt')` |
| `shutil.copy()` | 复制文件 | `shutil.copy('src.txt','dst.txt')` |
---
### **六、高阶函数**
| 函数 | 说明 | 示例 |
|----------------------|-----------------------------|-------------------------------|
| `map()` | 对可迭代对象应用函数 | `list(map(str, [1,2,3]))` → `['1','2','3']` |
| `filter()` | 过滤元素 | `list(filter(lambda x: x>0, [-1,0,1]))` → `[1]` |
| `reduce()` | 累积计算(需`from functools import reduce`) | `reduce(lambda x,y: x+y, [1,2,3])` → 6 |
| `lambda` | 匿名函数 | `(lambda x: x*2)(3)` → 6 |
---
### **七、常用模块函数**
- **`math`模块**:`sqrt()`, `ceil()`, `floor()`, `pi`
- **`random`模块**:`random()`, `randint()`, `choice()`
- **`datetime`模块**:`datetime.now()`, `timedelta`
- **`json`模块**:`json.dumps()`, `json.loads()`
---
### **最佳实践提示**
1. 使用`help()`查看函数文档:`help(print)`
2. 避免重复造轮子,优先使用内置函数(性能更优)
3. 组合函数提高效率(如`map`+`filter`)
掌握这些函数能覆盖80%的日常编码需求! 🚀