首页 > 数据库 > 数据库开发技术 > Python常用内置函数使用介绍
Python 提供了许多内置函数,这些函数可以帮助我们更高效地编写代码。熟悉并掌握Python内置函数,不仅可以提高编程效率,还能让你的代码更加简洁、易读。本文介绍这些常用的内置函数供大家学习。

len(): 计算字符串、列表、元组等对象的长度my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # 输出:5
sorted(): 对可迭代对象进行排序,返回一个新的排序后的列表my_list = [3, 1, 4, 1, 5, 9]
sorted_list = sorted(my_list)
print(sorted_list) # 输出:[1, 1, 3, 4, 5, 9]
sum(): 计算数字列表的总和numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total) # 输出:15
min(): 返回可迭代对象中的最小值numbers = [3, 1, 4, 1, 5, 9]
smallest = min(numbers)
print(smallest) # 输出:1
max(): 返回可迭代对象中的最大值numbers = [3, 1, 4, 1, 5, 9]
largest = max(numbers)
print(largest) # 输出:9
abs(): 返回数字的绝对值x = -5
print(abs(x)) # 输出:5
round(): 四舍五入浮点数到指定的小数位数x = 3.14159
print(round(x, 2)) # 输出:3.14
pow(): 计算一个数的幂次方x = 2
y = 3
result = pow(x, y)
print(result) # 输出:8
range(): 生成一个整数序列for i in range(5):
print(i) # 输出:0 1 2 3 4
enumerate(): 将一个可遍历的数据对象组合为一个索引序列,同时列出数据和数据下标fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)
# 输出:
# 0 apple
# 1 banana
# 2 cherry
zip(): 将两个或多个可迭代对象的元素打包成一个个元组,返回由这些元组组成的列表names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
zipped = zip(names, ages)
print(list(zipped)) # 输出:[('Alice', 25), ('Bob', 30), ('Charlie', 35)]
reversed(): 反转一个序列并返回一个反向迭代器my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))
print(reversed_list) # 输出:[5, 4, 3, 2, 1]
str(): 将对象转换为字符串num = 42
str_num = str(num)
print(str_num) # 输出:"42"
list(): 将可迭代对象转换为列表tup = (1, 2, 3)
lst = list(tup)
print(lst) # 输出:[1, 2, 3]
tuple(): 将可迭代对象转换为元组my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple) # 输出:(1, 2, 3)
set(): 创建一个无序不重复元素集my_list = [1, 2, 2, 3, 4, 4, 5]
unique_elements = set(my_list)
print(unique_elements) # 输出:{1, 2, 3, 4, 5}
dict(): 创建字典keys = ['a', 'b', 'c']
values = [1, 2, 3]
my_dict = dict(zip(keys, values))
print(my_dict) # 输出:{'a': 1, 'b': 2, 'c': 3}
all(): 如果所有元素都为真,则返回True,否则返回Falsemy_list = [True, True, True]
print(all(my_list)) # 输出:True
any(): 如果至少有一个元素为真,则返回True,否则返回Falsemy_list = [False, False, True]
print(any(my_list)) # 输出:True
bin(): 将整数转换为二进制字符串print(bin(10)) # 输出: '0b1010'
bool(): 将给定的值转换为布尔值print(bool(1)) # 输出:True
print(bool(0)) # 输出:False
chr(): 返回指定 Unicode 码点的字符print(chr(65)) # 输出: 'A'
divmod(): 返回商和余数的元组print(divmod(10, 3)) # 输出: (3, 1)
eval(): 计算字符串中的表达式并返回结果expression = "3 + 4"
print(eval(expression)) # 输出:7
filter(): 根据指定的条件过滤可迭代对象的元素numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # 输出: [2, 4]
float(): 将给定的值转换为浮点数print(float(3)) # 输出:3.0
format(): 格式化字符串print("Hello, {name}!".format(name="World")) # 输出:Hello, World!
input(): 从用户获取输入(在交互式环境中使用)user_input = input("Enter your name: ")
print("Hello,", user_input)
int(): 将给定的值转换为整数print(int("123")) # 输出:123
find(): 用于在字符串中查找子串首次出现的位置。如果找到子串,则返回其开始的索引;如果未找到,则返回-1。# 定义一个字符串
text = "Hello, welcome to the world of Python."
# 使用find方法查找子串"world"
index_of_world = text.find("world")
# 输出结果
print("The index of 'world' in the text is:", index_of_world)
# 输出:The index of 'world' in the text is: 22
通过合理地运用本文介绍的这些内置函数,可以更专注于解决实际问题,提高编程的效率。
原文作者:Python学习杂记;如有侵权,请联系管理员删除。