【学习】Python内置函数详解与实用指南
前言
Python内置函数是Python编程语言中预先定义的函数,它们嵌入到Python解释器中,任何时候都能直接使用,无需导入任何模块。这些内置函数的存在极大地提升了程序员的开发效率和程序的可读性。掌握Python内置函数是每个Python开发者必备的基础技能。本文将系统地介绍Python中最常用的内置函数,并提供实用的代码示例。
一、数学运算函数
(一)abs() - 绝对值函数
abs()
函数返回数字的绝对值。
# 整数绝对值
print(abs(-5)) # 输出: 5
print(abs(5)) # 输出: 5
# 浮点数绝对值
print(abs(-3.14)) # 输出: 3.14
# 复数的模
print(abs(3+4j)) # 输出: 5.0
(二)round() - 四舍五入函数
round()
函数对数字进行四舍五入。
# 基本四舍五入
print(round(3.14159)) # 输出: 3
print(round(3.14159, 2)) # 输出: 3.14
print(round(3.14159, 4)) # 输出: 3.1416
# 负数四舍五入
print(round(-2.675, 2)) # 输出: -2.67
(三)pow() - 幂运算函数
pow()
函数计算x的y次幂。
# 基本幂运算
print(pow(2, 3)) # 输出: 8
print(pow(2, -1)) # 输出: 0.5
# 带模运算的幂运算
print(pow(2, 3, 5)) # 输出: 3 (2^3 % 5)
(四)sum() - 求和函数
sum()
函数对可迭代对象中的数字进行求和。
# 列表求和
numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # 输出: 15
# 带初始值的求和
print(sum(numbers, 10)) # 输出: 25
# 元组求和
print(sum((1, 2, 3))) # 输出: 6
(五)min() 和 max() - 最值函数
min()
和max()
函数分别返回最小值和最大值。
# 多个参数的最值
print(min(1, 5, 3, 9)) # 输出: 1
print(max(1, 5, 3, 9)) # 输出: 9
# 可迭代对象的最值
numbers = [3, 1, 4, 1, 5, 9]
print(min(numbers)) # 输出: 1
print(max(numbers)) # 输出: 9
# 字符串的最值(按ASCII码)
print(min('hello')) # 输出: 'e'
print(max('hello')) # 输出: 'o'
二、类型转换函数
(一)int() - 整数转换
int()
函数将其他类型转换为整数。
# 浮点数转整数
print(int(3.14)) # 输出: 3
print(int(-2.8)) # 输出: -2
# 字符串转整数
print(int('123')) # 输出: 123
print(int('-456')) # 输出: -456
# 不同进制转换
print(int('1010', 2)) # 输出: 10 (二进制转十进制)
print(int('FF', 16)) # 输出: 255 (十六进制转十进制)
print(int('77', 8)) # 输出: 63 (八进制转十进制)
(二)float() - 浮点数转换
float()
函数将其他类型转换为浮点数。
# 整数转浮点数
print(float(5)) # 输出: 5.0
# 字符串转浮点数
print(float('3.14')) # 输出: 3.14
print(float('-2.5')) # 输出: -2.5
# 科学计数法
print(float('1e-3')) # 输出: 0.001
print(float('2.5e2')) # 输出: 250.0
(三)str() - 字符串转换
str()
函数将其他类型转换为字符串。
# 数字转字符串
print(str(123)) # 输出: '123'
print(str(3.14)) # 输出: '3.14'
# 布尔值转字符串
print(str(True)) # 输出: 'True'
print(str(False)) # 输出: 'False'
# 列表转字符串
print(str([1, 2, 3])) # 输出: '[1, 2, 3]'
(四)bool() - 布尔值转换
bool()
函数将其他类型转换为布尔值。
# 数字转布尔值
print(bool(1)) # 输出: True
print(bool(0)) # 输出: False
print(bool(-1)) # 输出: True
# 字符串转布尔值
print(bool('hello')) # 输出: True
print(bool('')) # 输出: False
# 容器转布尔值
print(bool([1, 2, 3])) # 输出: True
print(bool([])) # 输出: False
print(bool({'a': 1})) # 输出: True
print(bool({})) # 输出: False
(五)list()、tuple()、set() - 容器类型转换
这些函数用于在不同容器类型之间进行转换。
# 字符串转列表
print(list('hello')) # 输出: ['h', 'e', 'l', 'l', 'o']
# 元组转列表
print(list((1, 2, 3))) # 输出: [1, 2, 3]
# 列表转元组
print(tuple([1, 2, 3])) # 输出: (1, 2, 3)
# 列表转集合(去重)
print(set([1, 2, 2, 3, 3])) # 输出: {1, 2, 3}
# 字符串转集合
print(set('hello')) # 输出: {'h', 'e', 'l', 'o'}
三、序列操作函数
(一)len() - 长度函数
len()
函数返回对象的长度或元素个数。
# 字符串长度
print(len('hello')) # 输出: 5
print(len('你好世界')) # 输出: 4
# 列表长度
print(len([1, 2, 3, 4])) # 输出: 4
# 字典长度
print(len({'a': 1, 'b': 2})) # 输出: 2
# 集合长度
print(len({1, 2, 3})) # 输出: 3
(二)range() - 范围函数
range()
函数生成数字序列。
# 基本用法
print(list(range(5))) # 输出: [0, 1, 2, 3, 4]
print(list(range(1, 6))) # 输出: [1, 2, 3, 4, 5]
print(list(range(0, 10, 2))) # 输出: [0, 2, 4, 6, 8]
# 倒序范围
print(list(range(10, 0, -1))) # 输出: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# 在循环中使用
for i in range(3):
print(f"第{i+1}次循环")
(三)enumerate() - 枚举函数
enumerate()
函数为可迭代对象添加索引。
# 基本用法
fruits = ['apple', 'banana', 'orange']
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# 输出:
# 0: apple
# 1: banana
# 2: orange
# 指定起始索引
for index, fruit in enumerate(fruits, start=1):
print(f"{index}: {fruit}")
# 输出:
# 1: apple
# 2: banana
# 3: orange
(四)zip() - 打包函数
zip()
函数将多个可迭代对象打包成元组。
# 基本用法
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 输出:
# Alice is 25 years old
# Bob is 30 years old
# Charlie is 35 years old
# 多个序列打包
scores1 = [85, 90, 78]
scores2 = [88, 92, 80]
scores3 = [90, 85, 82]
for s1, s2, s3 in zip(scores1, scores2, scores3):
print(f"总分: {s1 + s2 + s3}")
(五)sorted() - 排序函数
sorted()
函数对可迭代对象进行排序。
# 基本排序
numbers = [3, 1, 4, 1, 5, 9]
print(sorted(numbers)) # 输出: [1, 1, 3, 4, 5, 9]
# 倒序排序
print(sorted(numbers, reverse=True)) # 输出: [9, 5, 4, 3, 1, 1]
# 字符串排序
words = ['banana', 'apple', 'cherry']
print(sorted(words)) # 输出: ['apple', 'banana', 'cherry']
# 按长度排序
print(sorted(words, key=len)) # 输出: ['apple', 'banana', 'cherry']
# 复杂对象排序
students = [('Alice', 85), ('Bob', 90), ('Charlie', 78)]
print(sorted(students, key=lambda x: x[1])) # 按成绩排序
# 输出: [('Charlie', 78), ('Alice', 85), ('Bob', 90)]
(六)reversed() - 反转函数
reversed()
函数返回反转的迭代器。
# 列表反转
numbers = [1, 2, 3, 4, 5]
print(list(reversed(numbers))) # 输出: [5, 4, 3, 2, 1]
# 字符串反转
print(list(reversed('hello'))) # 输出: ['o', 'l', 'l', 'e', 'h']
print(''.join(reversed('hello'))) # 输出: 'olleh'
# 在循环中使用
for item in reversed([1, 2, 3]):
print(item) # 输出: 3, 2, 1
四、逻辑判断函数
(一)all() - 全部为真判断
all()
函数判断可迭代对象中的所有元素是否都为真。
# 基本用法
print(all([True, True, True])) # 输出: True
print(all([True, False, True])) # 输出: False
print(all([])) # 输出: True (空序列)
# 数字判断
print(all([1, 2, 3])) # 输出: True
print(all([1, 0, 3])) # 输出: False
# 字符串判断
print(all(['hello', 'world'])) # 输出: True
print(all(['hello', ''])) # 输出: False
# 实际应用:检查所有学生是否及格
scores = [85, 90, 78, 92]
print(all(score >= 60 for score in scores)) # 输出: True
(二)any() - 任一为真判断
any()
函数判断可迭代对象中是否有任一元素为真。
# 基本用法
print(any([False, False, True])) # 输出: True
print(any([False, False, False])) # 输出: False
print(any([])) # 输出: False (空序列)
# 数字判断
print(any([0, 0, 1])) # 输出: True
print(any([0, 0, 0])) # 输出: False
# 实际应用:检查是否有学生不及格
scores = [85, 45, 78, 92]
print(any(score < 60 for score in scores)) # 输出: True
五、输入输出函数
(一)print() - 输出函数
print()
函数用于输出信息到控制台。
# 基本输出
print("Hello, World!")
# 多个参数输出
print("Name:", "Alice", "Age:", 25)
# 输出: Name: Alice Age: 25
# 自定义分隔符
print("apple", "banana", "orange", sep=", ")
# 输出: apple, banana, orange
# 自定义结束符
print("Hello", end=" ")
print("World")
# 输出: Hello World
# 输出到文件
with open("output.txt", "w") as f:
print("Hello, File!", file=f)
(二)input() - 输入函数
input()
函数用于从用户获取输入。
# 基本输入
name = input("请输入您的姓名: ")
print(f"您好, {name}!")
# 数字输入(需要类型转换)
age = int(input("请输入您的年龄: "))
print(f"您今年{age}岁")
# 多个值输入
numbers = input("请输入多个数字(用空格分隔): ").split()
numbers = [int(x) for x in numbers]
print(f"您输入的数字是: {numbers}")
六、高级函数
(一)map() - 映射函数
map()
函数将函数应用到可迭代对象的每个元素。
# 基本用法
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
print(squares) # 输出: [1, 4, 9, 16, 25]
# 字符串转换
str_numbers = ['1', '2', '3', '4', '5']
int_numbers = list(map(int, str_numbers))
print(int_numbers) # 输出: [1, 2, 3, 4, 5]
# 多个序列映射
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list(map(lambda x, y: x + y, list1, list2))
print(result) # 输出: [5, 7, 9]
(二)filter() - 过滤函数
filter()
函数过滤可迭代对象中满足条件的元素。
# 基本用法
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # 输出: [2, 4, 6, 8, 10]
# 过滤字符串
words = ['apple', 'banana', 'cherry', 'date']
long_words = list(filter(lambda x: len(x) > 5, words))
print(long_words) # 输出: ['banana', 'cherry']
# 过滤None值
data = [1, None, 3, None, 5]
filtered_data = list(filter(None, data))
print(filtered_data) # 输出: [1, 3, 5]
(三)isinstance() - 类型检查函数
isinstance()
函数检查对象是否为指定类型的实例。
# 基本类型检查
print(isinstance(5, int)) # 输出: True
print(isinstance(5.0, float)) # 输出: True
print(isinstance('hello', str)) # 输出: True
print(isinstance([1, 2, 3], list)) # 输出: True
# 多类型检查
print(isinstance(5, (int, float))) # 输出: True
print(isinstance(5.0, (int, float))) # 输出: True
print(isinstance('5', (int, float))) # 输出: False
# 实际应用:类型安全的函数
def safe_divide(a, b):
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
raise TypeError("参数必须是数字类型")
if b == 0:
raise ValueError("除数不能为零")
return a / b
(四)hasattr()、getattr()、setattr() - 属性操作函数
这些函数用于动态操作对象的属性。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("Alice", 25)
# 检查属性是否存在
print(hasattr(person, 'name')) # 输出: True
print(hasattr(person, 'salary')) # 输出: False
# 获取属性值
print(getattr(person, 'name')) # 输出: Alice
print(getattr(person, 'salary', 0)) # 输出: 0 (默认值)
# 设置属性值
setattr(person, 'salary', 50000)
print(person.salary) # 输出: 50000
七、其他常用函数
(一)id() - 对象标识函数
id()
函数返回对象的唯一标识符。
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(id(a)) # 输出: 某个内存地址
print(id(b)) # 输出: 不同的内存地址
print(id(c)) # 输出: 与a相同的内存地址
print(a is c) # 输出: True
print(a is b) # 输出: False
print(a == b) # 输出: True
(二)type() - 类型获取函数
type()
函数返回对象的类型。
print(type(5)) # 输出: <class 'int'>
print(type(5.0)) # 输出: <class 'float'>
print(type('hello')) # 输出: <class 'str'>
print(type([1, 2, 3])) # 输出: <class 'list'>
# 类型比较
if type(5) == int:
print("5是整数")
# 注意:推荐使用isinstance()而不是type()进行类型检查
(三)dir() - 属性列表函数
dir()
函数返回对象的所有属性和方法列表。
# 查看字符串的方法
print(dir(str))
# 查看列表的方法
print(dir(list))
# 查看当前模块的属性
print(dir())
# 查看对象的属性
class MyClass:
def __init__(self):
self.attr1 = "value1"
def method1(self):
pass
obj = MyClass()
print(dir(obj))
(四)help() - 帮助函数
help()
函数显示对象的帮助信息。
# 查看函数帮助
help(len)
help(print)
# 查看模块帮助
import math
help(math)
# 查看类的帮助
help(list)
help(dict)
八、实际应用示例
(一)数据处理示例
# 学生成绩统计
students = [
{'name': 'Alice', 'scores': [85, 90, 78]},
{'name': 'Bob', 'scores': [92, 88, 95]},
{'name': 'Charlie', 'scores': [76, 82, 79]}
]
# 计算每个学生的平均分
for student in students:
avg_score = sum(student['scores']) / len(student['scores'])
student['average'] = round(avg_score, 2)
print(f"{student['name']}的平均分: {student['average']}")
# 找出最高平均分
best_student = max(students, key=lambda x: x['average'])
print(f"最高平均分学生: {best_student['name']} ({best_student['average']}分)")
# 检查是否所有学生都及格(平均分>=60)
all_passed = all(student['average'] >= 60 for student in students)
print(f"所有学生都及格: {all_passed}")
(二)文本处理示例
# 文本统计分析
text = "Python is a powerful programming language. Python is easy to learn."
# 转换为小写并分割单词
words = text.lower().replace('.', '').replace(',', '').split()
print(f"总单词数: {len(words)}")
# 统计每个单词出现次数
word_count = {}
for word in words:
word_count[word] = word_count.get(word, 0) + 1
# 按出现次数排序
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
print("单词频率统计:")
for word, count in sorted_words:
print(f"{word}: {count}")
# 找出长度大于5的单词
long_words = list(filter(lambda x: len(x) > 5, set(words)))
print(f"长单词: {long_words}")
(三)数据验证示例
# 用户输入验证
def validate_user_data(data):
"""
验证用户数据的完整性和有效性
"""
required_fields = ['name', 'age', 'email']
# 检查必填字段
missing_fields = [field for field in required_fields if not hasattr(data, field) or not getattr(data, field)]
if missing_fields:
return False, f"缺少必填字段: {', '.join(missing_fields)}"
# 验证年龄
if not isinstance(data.age, int) or data.age < 0 or data.age > 150:
return False, "年龄必须是0-150之间的整数"
# 验证邮箱格式(简单验证)
if '@' not in data.email or '.' not in data.email:
return False, "邮箱格式不正确"
return True, "验证通过"
# 使用示例
class UserData:
def __init__(self, name, age, email):
self.name = name
self.age = age
self.email = email
user1 = UserData("Alice", 25, "alice@example.com")
user2 = UserData("", -5, "invalid-email")
print(validate_user_data(user1)) # (True, '验证通过')
print(validate_user_data(user2)) # (False, '缺少必填字段: name')
九、性能优化建议
(一)选择合适的内置函数
# 推荐:使用内置函数
numbers = [1, 2, 3, 4, 5]
total = sum(numbers) # 快速且简洁
# 不推荐:手动循环
total = 0
for num in numbers:
total += num # 较慢且冗长
# 推荐:使用any()和all()
scores = [85, 90, 78, 92]
has_excellent = any(score >= 90 for score in scores)
all_passed = all(score >= 60 for score in scores)
# 不推荐:手动检查
has_excellent = False
for score in scores:
if score >= 90:
has_excellent = True
break
(二)避免不必要的类型转换
# 推荐:直接使用合适的类型
numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
# 不推荐:不必要的转换
numbers = ['1', '2', '3', '4', '5']
result = sum(int(x) for x in numbers) # 如果可以直接使用整数列表
十、常见错误与注意事项
(一)类型错误
# 错误示例:类型不匹配
try:
result = sum(['1', '2', '3']) # TypeError
except TypeError as e:
print(f"错误: {e}")
# 正确做法
result = sum(int(x) for x in ['1', '2', '3'])
print(f"正确结果: {result}")
(二)空序列处理
# 处理空序列
empty_list = []
# 安全的做法
if empty_list:
max_value = max(empty_list)
else:
max_value = None # 或其他默认值
# 或者使用异常处理
try:
max_value = max(empty_list)
except ValueError:
max_value = None
(三)迭代器消耗
# 注意:某些函数返回迭代器,只能使用一次
numbers = [1, 2, 3, 4, 5]
filtered = filter(lambda x: x > 2, numbers)
print(list(filtered)) # [3, 4, 5]
print(list(filtered)) # [] - 迭代器已被消耗
# 如需多次使用,转换为列表
filtered = list(filter(lambda x: x > 2, numbers))
print(filtered) # [3, 4, 5]
print(filtered) # [3, 4, 5] - 可以重复使用
总结
Python内置函数是Python编程的基础工具,掌握这些函数能够显著提高编程效率和代码质量。本文介绍了最常用的内置函数,包括数学运算、类型转换、序列操作、逻辑判断、输入输出和高级函数等类别。
在实际开发中,建议:
- 优先使用内置函数:它们经过优化,性能更好
- 理解函数特性:了解返回值类型和可能的异常
- 合理组合使用:多个内置函数组合能解决复杂问题
- 注意类型安全:使用isinstance()进行类型检查
- 处理边界情况:考虑空序列、None值等特殊情况
通过系统学习和实践这些内置函数,能够写出更加Pythonic、高效和可读的代码。
参考资料
- Python官方文档 - 内置函数:https://docs.python.org/zh-cn/3/library/functions.html
3 - 菜鸟教程 - Python内置函数:https://www.runoob.com/python/python-built-in-functions.html
1 - 知乎专栏 - Python内置函数详解:https://zhuanlan.zhihu.com/p/341323946
2