11_函数
创建和调用函数
# I Love You # I Love You # I Love You def myfunc(): for i in range(3): print("I Love You") myfunc()
函数的参数
# I Love python # I Love python # I Love python # I Love python # I Love python def myfunc(name,time): for i in range(time): print(f"I Love {name}") myfunc("python",5)
函数返回
def div(x, y): z = x / y return z div(4, 2) ----------------------------- def div(x, y): if y == 0: return "除数不能为0!" else: return x / y print(div(4, 2))
函数参数
# 位置参数 def myfunc(s, vt, o): return "".join((o, vt, s)) print(myfunc("我", "打了", "你")) # 关键字参数 print(myfunc(o="我", vt="打了", s="你")) # 位置参数必须在关键字参数之前 print(myfunc("我", vt="打了", s="你")) # 默认参数 def myfunc(s, vt, o = "她"): return "".join((o, vt, s)) print(myfunc("我", "打了")) ---------------------------------------- # 收集参数 print() def myfunc(*args): # 有2个参数。 # 第2个参数是:我打算 print("有{}个参数。".format(len(args))) print("第2个参数是:{}".format(args[1])) myfunc("小甲鱼", "我打算") # 利用元组进行打包 # 返回值是一个元组(1, 2, 3) def myfunc(): return 1, 2, 3 print(myfunc()) # 元组解包 x, y, z = myfunc() print(x) print(y) print(z) # (1, 2, 3) 4 5 def myfunc(*args, a, b): print(args, a, b) myfunc(1, 2, 3, a=4, b=5) # 收集参数打包成字典 def myfunc(**kwargs): print(kwargs) # {'a': 1, 'b': 2, 'c': 3} myfunc(a=1, b=2, c=3) def myfunc(a, *b, **c): print(a, b, c) # 1 (2, 3, 4) {'x': 5, 'y': 6} myfunc(1, 2, 3, 4, x=5, y=6) # 解包参数 args = (1, 2, 3, 4) def myfunc(a, b, c, d): print(a, b, c, d) # 1 2 3 4 myfunc(*args) kwargs = {'a': 1, 'b': 2, 'c': 3, 'd': 4} # 1 2 3 4 myfunc(**kwargs) -----------------------------------------------
作用域
# 全局变量 x = 520 def myfunc(): # 局部变量 x = 1314 print(x) # 1314 myfunc() # 520 print(x) # 在函数里面修改全局变量,global修改的是函数体外的全局变量 x = 1314 def myfunc(): global x x = 520 print(x) # 520 myfunc() # 520,此时的全局变量已经在函数里面修改了 print(x) # 嵌套函数,nonlocal 修改的是上一层嵌套的函数变量,若有多层嵌套也只修改上一层,上一层要是没有局部变量,直接报错 def funA(): x = 520 def funB(): x = 1314 print("In funB, x =", x) # In funB, x = 1314 funB() print("In funA, x =", x) # In funA, x = 520 funA() def funA(): x = 520 def funB(): nonlocal x x = 1314 print("In funB, x =", x) # In funB, x = 1314 funB() print("In funA, x =", x) # In funA, x = 1314,此时上一层嵌套的函数变量被函数体修改 funA() --------------------------------------------------------
闭包
def power(exp): def exp_of(base): # 幂运算 return base ** exp return exp_of square = power(2) cube = power(3) # 4 print(square(2)) # 25 print(square(5)) # 8 print(cube(2)) # 125 print(cube(5)) def outer(): x = 0 y = 0 def inner(x1, y1): nonlocal x, y x += x1 y += y1 print(f"现在,x = {x}, y = {y}") return inner move = outer() # 现在,x = 1, y = 2 move(1, 2) # 现在,x = -1, y = 4 move(-2, 2)
装饰器
def myfunc(): print("正在调用myfunc....") def report(func): print("开始调用函数...") func() print("调用函数完毕!") # 开始调用函数... # 正在调用myfunc.... # 调用函数完毕! report(myfunc) ----------------------------------------- # 装饰器 @time_master def myfunc(): time.sleep(2) print("hello,love")
时间管理大师函数
import time def time_master(func): print("开始运行程序>>>") start = time.time() func() stop = time.time() print("结束程序运行>>>") print(f"一共耗时:{(stop-start):.2f}秒") def myfunc(): time.sleep(2) print("hello,love") time_master(myfunc)
lambda表达式
# 常规函数实现求平方 def squareX(x): return x * x print(squareX(3)) # lambda表达式实现 squareY = lambda y : y * y print(squareY(3))
生成器
def fib(): back1, back2 = 0, 1 while True: yield back1 back1, back2 = back2, back1 + back2 f = fib() # 0 # 1 # 1 # 2 # 3 print(next(f)) print(next(f)) print(next(f)) print(next(f)) print(next(f)) --------------------------------------------- def funA(): print('我是A函数') def funB(): funA() def funC(i): if i > 0: print("我是C函数") i -= 1 funC(i) funC(10)
函数文档
# help(print()) def exchange(dollar, rate = 6.32): """ 功能:汇率换算,美元-> 人民币 参数 - dollar: 美元数量 - rate: 汇率,默认值是 6.32(2022-7-5) 返回值: - 人民币的数量 """ return dollar * rate print(exchange(20)) (help(exchange)) # 类型注释 def times(s:str, n:int) -> str: return s * n # lovelovelovelovelove print(times("love", 5)) def times(s:list, n:int = 3) -> list: return s * n # [1, 2, 3, 1, 2, 3, 1, 2, 3] print(times([1, 2, 3])) def times(s:dict[str,int], n:int = 3) -> list: return list(s.keys()) * n # ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'] print(times({'A':1,'B':2,'C':3})) # 内省 # {'s': dict[str, int], 'n': <class 'int'>, 'return': <class 'list'>} print(times.__annotations__) # 功能:汇率换算,美元-> 人民币 # 参数 # - dollar: 美元数量 # - rate: 汇率,默认值是 6.32(2022-7-5) # 返回值: # - 人民币的数量 print(exchange.__doc__)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律