欢迎莅临我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。
持续学习,不断总结,共同进步,为了踏实,做好当下事儿~
非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。 ✨✨ 欢迎订阅本专栏 ✨✨
The Start点点关注,收藏不迷路
|
Python作为一门简洁高效的编程语言,凭借其丰富的内置函数和标准库函数,成为开发者日常工作的利器。本文将系统性地解析100个最常用的Python函数,涵盖字符串处理、数据结构操作、文件IO、数学计算等核心领域,帮助读者快速掌握Python编程的精髓。
函数是组织好的、可重复使用的代码块,用于实现单一或相关联功能。
定义与作用:
def greet(name):
return f"Hello, {name}!"
通过def
关键字声明,return
返回结果。优势包括代码复用(减少重复)和模块化(分解复杂任务)。
返回值:若无return
,函数默认返回None
。多返回值可通过元组实现:
def get_coordinates():
return 10, 20 # 等价于 (10, 20)
func(1, 2)
。func(a=1, b=2)
。def power(x, n=2): # n默认为2
return x ** n
*args
接收任意数量位置参数(元组形式)**kwargs
接收关键字参数(字典形式)def log(*args, **kwargs):
print("Args:", args)
print("KWargs:", kwargs)
len("Python")
→ 6"hello".upper()
→ "HELLO"
" text ".strip()
→ "text"
"apple".find("p")
→ 1(未找到返回-1)"hello".replace("l", "L")
→ "heLLo"
split
与join
组合:words = "a,b,c".split(",") # ["a", "b", "c"]
"-".join(words) # "a-b-c"
"{} + {} = {}".format(1, 2, 3) # "1 + 2 = 3"
"123".isdigit()
→ True
(是否全为数字)lst = [1, 2]
lst.append(3) # [1, 2, 3]
lst.extend([4,5])# [1, 2, 3, 4, 5]
sorted([3, 1, 2]) # 新列表 [1, 2, 3]
lst.sort() # 原地排序
d = {"a": 1}
d.keys() # ["a"]
d.get("b", 0) # 键不存在返回0(避免KeyError)
d1.update({"b": 2}) # d1变为 {"a":1, "b":2}
{1, 2} | {2, 3} # 并集 {1, 2, 3}
(1, 2, 2).count(2)
→ 2with open("file.txt", "r") as f:
content = f.read() # 读取全部内容
r
(读)、w
(写,覆盖)、a
(追加)import os
os.path.join("dir", "file.txt") # "dir/file.txt"(Linux/Mac)
os.listdir("/path")
返回文件名列表abs(-5) # 5
round(3.1415, 2) # 3.14
sum([1, 2, 3]) # 6
import math
math.sqrt(16) # 4.0
from datetime import datetime
datetime.now() # 2023-10-01 12:00:00
datetime.now().strftime("%Y-%m-%d") # "2023-10-01"
map
与filter
:list(map(lambda x: x*2, [1, 2, 3])) # [2, 4, 6]
list(filter(lambda x: x>1, [1, 2, 3])) # [2, 3]
isinstance(10, int) # True
help(str.split)
查看函数说明split
)、数据结构(如dict.get
)、文件IO(如open
)等。help()
查阅官方文档collections
、itertools
等标准库numpy
、pandas
等第三方库通过掌握这100个核心函数,你将能够高效处理大多数Python编程任务,并为深入开发打下坚实基础。
道阻且长,行则将至,让我们一起加油吧!
The Start点点关注,收藏不迷路
|
The Start点点关注,收藏不迷路