ITPub博客

首页 > 数据库 > 数据库开发技术 > Python常用内置函数使用介绍

Python常用内置函数使用介绍

原创 数据库开发技术 作者:derekwh 时间:2024-07-01 08:34:20 0 删除 编辑

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

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

请登录后发表评论 登录
全部评论
央企算法工程师,微信公众号: python数智工坊

注册时间:2021-03-14

  • 博文量
    305
  • 访问量
    162362