苦练Python第17天:你必须掌握的Python内置函数
作者:Rahul Gupta
译者:倔强青铜三
前言
大家好,我是倔强青铜三 。是一名热情的软件工程师,我热衷于分享和传播IT技术,致力于通过我的知识和技能推动技术交流与创新,欢迎关注我,微信公众号:倔强青铜三。欢迎点赞、收藏、关注,一键三连!!!
欢迎来到 100 天 Python 挑战 的 第 17 天 !
今天,我们开箱即用------聚焦 Python 内置函数(built-in functions)。它们无需安装、不必导入,随时待命,让代码更短、更快、更易读。
📦 今日收获清单
- 什么是内置函数
- 20+ 高频内置函数速查表
- 真实场景示例
- 彩蛋:探索更多隐藏宝藏
🤔 内置函数是什么?
内置函数 是 Python 启动即加载的 预定义函数 。
无需 import
,直接呼唤即可。
python
print("Hello, Python!") # ✅ print 就是内置函数
🚀 高频内置函数速通榜(附示例)
1️⃣ print()
-- 输出利器
python
print("Learning Python!")
2️⃣ len()
-- 长度侦探
python
name = "Python"
print(len(name)) # 6
3️⃣ type()
-- 类型照妖镜
python
print(type(5)) # <class 'int'>
print(type("Hello")) # <class 'str'>
4️⃣ int()
/ float()
/ str()
-- 类型变形术
python
age = "25"
print(int(age)) # 25
5️⃣ input()
-- 互动之门
python
name = input("Enter your name: ")
print("Hello,", name)
6️⃣ sum()
-- 一键求和
python
numbers = [10, 20, 30]
print(sum(numbers)) # 60
7️⃣ max()
/ min()
-- 极值秒取
python
print(max([3, 6, 2])) # 6
print(min([3, 6, 2])) # 2
8️⃣ sorted()
-- 排序瞬发
python
print(sorted([5, 2, 9])) # [2, 5, 9]
9️⃣ range()
-- 数字工厂
python
for i in range(3):
print(i) # 0 1 2
🔟 enumerate()
-- 索引伴侣
python
colors = ["red", "blue", "green"]
for index, color in enumerate(colors):
print(index, color)
1️⃣1️⃣ zip()
-- 并行缝合
python
names = ["Alice", "Bob"]
scores = [85, 92]
for name, score in zip(names, scores):
print(f"{name} scored {score}")
1️⃣2️⃣ abs()
-- 绝对值
python
print(abs(-7)) # 7
1️⃣3️⃣ round()
-- 精准四舍五入
python
print(round(3.14159, 2)) # 3.14
1️⃣4️⃣ all()
/ any()
-- 批量真假判官
python
print(all([True, True, False])) # False
print(any([False, False, True])) # True
1️⃣5️⃣ dir()
-- 属性导航仪
python
print(dir(str)) # 所有字符串方法一览
1️⃣6️⃣ help()
-- 在线文档
python
help(len) # len() 详细说明书
1️⃣7️⃣ eval()
-- 代码即字符串(⚠️谨慎使用)
python
expression = "3 + 5"
print(eval(expression)) # 8
1️⃣8️⃣ reversed()
-- 逆序魔法
python
for char in reversed("Python"):
print(char, end="") # nohtyP
1️⃣9️⃣ isinstance()
-- 类型安检
python
x = 10
print(isinstance(x, int)) # True
2️⃣0️⃣ map()
/ filter()
-- 函数式神器
python
nums = [1, 2, 3]
squared = list(map(lambda x: x**2, nums))
print(squared) # [1, 4, 9]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # [2]
💡 彩蛋:一键查看全部内置函数
python
print(dir(__builtins__))
🧠 一日精华
- 内置函数:开机自带、零成本
- 掌握 20+ 高频利器,代码量骤减
help()
&dir()
:官方文档随身带- 立即调用,拒绝重复造轮子
最后感谢阅读!欢迎关注我,微信公众号 :
倔强青铜三
。欢迎点赞
、收藏
、关注
,一键三连!!!