• 函数


    # 函数 (风湿理论:函数即变量)
    # 返回值数=0:返回None; 返回值=1:返回object; 返回值>1:返回tuple
    
    # 位置参数与关键字参数
    def test(x, y, z):
        pass
    test(1, 2, 3)  # 位置参数一一对应,缺一不可,多一不可
    test(y=2, z=3, x=1)  # 关键字参数,无须一一对应,缺一不可,多一不可
    test(1, z=3, y=2)  # 混用,位置参数必须在关键字参数的左边
    
    
    # 默认参数 默认参数必须在非默认参数的后面
    def handle(x, type='mysql'):
        print(x, type)
    handle('你好')
    
    
    # 参数组(非固定长度的参数) *列表  **字典
    def test(x, *args):
        print(x, args)
    test(1, 2, 4, 5, [6, 7])
    
    
    def test(x, *args):
        print(x, args)
    test(1, *[2, 3, 4, 5, 6])
    
    
    def test(x, **kwargs):
        print(x, kwargs)
    test(1, y=2, z=3)
    
    
    def test(x, **kwargs):
        print(x, kwargs)
    test(1, **{"name": "张三", "age": 18})
    
    
    # 混合用法
    def test(x, *args, **kwargs):
        print(x)
        print(args)
        print(kwargs)
    test(1, *[1, 2, 3, 4, 5], **{"name": "张三", "age": 18})
    
    #全局变量与局部变量 规范:全局变量全大写,局部变量全小写
    # 在函数体中修改全局变量
    NAME = 'lhf'
    def changename():
        global NAME   #指对name的操作都是全局变量name
        NAME = '改名字'
        print(NAME)
    changename()
    print(NAME)
    
    #在函数体中修改上一级变量
    def test():
        name = '张三'
        def changename():
            nonlocal  name
            name =  '李四'
        changename()
        print(name)
    test()
    
    
    # 函数递归
    # 递归必须要有一个明确的结束条件(if    return)
    # 递归进入更深一层时,问题规模相比上次递归都应有所减少
    # 递归效率不高,递归层次过多会导致栈溢出
    def calc(n):
        print(n)
        if int(n / 2) == 0:
            return n
        return calc(int(n / 2))
    calc(100)
  • 相关阅读:
    c++检测本机网络
    ShellExecuteEx 阻塞和异步调用进程的两种方法
    QImage 转base64
    C 位域运算
    Linq 取差集 交集等
    Linq 筛选出一条数据
    Linq查询出结果集中重复数据
    使AspNetPager控件中文显示分页信息
    C盘瘦身,可以让你的电脑C盘恢复到刚安装时的大小
    Linq Distinct List 去重复
  • 原文地址:https://www.cnblogs.com/dangrui0725/p/9411130.html
Copyright © 2020-2023  润新知