Python3常见接口函数
一、基础内置函数
- 输入输出
print():输出内容input():读取用户输入
 - 类型转换
int()、float()、str()、bool():基础类型转换list()、tuple()、set()、dict():容器类型转换bin()、hex()、oct():进制转换
 - 数学运算
abs():绝对值round():四舍五入pow(x, y):计算x^ysum():求和min()、max():最小/最大值
 - 迭代与序列操作
len():获取长度range():生成整数序列enumerate():添加索引的迭代器sorted():排序reversed():反转序列zip():合并多个迭代器
 - 对象与反射
type():查看类型isinstance():类型检查id():对象内存地址dir():查看对象属性hasattr()、getattr()、setattr():动态操作属性
 - 文件操作
open():打开文件(常用模式:r/w/a/rb/wb)
 
二、常用标准库接口
1. os 模块(操作系统交互)
os.getcwd():当前工作目录os.listdir(path):列出目录内容os.mkdir(path)/os.makedirs(path):创建目录(递归创建用makedirs)os.remove(path):删除文件os.path.join(a, b):路径拼接os.path.exists(path):检查路径是否存在
2. sys 模块(系统参数)
sys.argv:命令行参数列表sys.exit():退出程序sys.version:Python 版本信息
3. json 模块(JSON 处理)
json.dumps(obj):对象 → JSON 字符串json.loads(s):JSON 字符串 → 对象json.dump(obj, file):写入 JSON 文件json.load(file):读取 JSON 文件
4. datetime 模块(时间处理)
datetime.now():当前时间datetime.strftime(format):时间 → 字符串datetime.strptime(str, format):字符串 → 时间
5. re 模块(正则表达式)
re.search(pattern, string):搜索匹配re.match(pattern, string):从头匹配re.findall(pattern, string):返回所有匹配re.sub(pattern, repl, string):替换匹配项
6. random 模块(随机数)
random.randint(a, b):生成整数[a, b]random.choice(seq):随机选择元素random.shuffle(seq):打乱序列
7. collections 模块(高效容器)
defaultdict:带默认值的字典Counter:计数器(统计元素频次)deque:双端队列
8. itertools 模块(迭代工具)
itertools.chain():合并多个迭代器itertools.product():笛卡尔积itertools.permutations():排列组合
三、高阶函数
- 函数式编程
map(func, iterable):对每个元素应用函数filter(func, iterable):过滤元素reduce(func, iterable):累积计算(需from functools import reduce)
 - 装饰器
@staticmethod/@classmethod:静态/类方法@property:定义属性
 
四、常用字符串方法
            
            
              python
              
              
            
          
          s = "Hello World"
s.strip()       # 去除两端空格
s.split()       # 分割字符串
s.replace(a, b) # 替换子串
s.startswith()  # 检查前缀
s.lower()       # 转小写
s.join(iter)    # 连接字符串
        五、文件读写示例
            
            
              py
              
              
            
          
          # 写入文件
with open("file.txt", "w") as f:
    f.write("Hello Python")
# 读取文件
with open("file.txt", "r") as f:
    content = f.read()
        最佳实践提示
- 上下文管理器 :使用 
with安全处理资源(文件、网络连接) - 列表推导式 :简化循环操作(如 
[x*2 for x in range(10)]) - 错误处理 :使用 
try...except捕获异常 
这些函数覆盖了日常开发中 80% 以上的需求,熟练掌握可大幅提升编码效率。建议结合官方文档实践使用!