• day8-函数


    ---def test(x):   # def:定义函数的关键字,test:函数名, x相当于以前函数中的自变量
    使用函数的好处:
    1.代码重用
    2.保持一致性,易于维护
    3.可扩展性
    def test(x):   # def:定义函数的关键字,test:函数名, x相当于以前函数中的自变量
        '''
        2*x+1
        :param x:整形数字
        :return: 返回计算结果
        '''
        y=2*x+1
        return y
    
    def test():
        '''
        2*x+1
        :param x:整形数字
        :return: 返回计算结果
        '''
        x=3
        y=2*x+1
        return y
    a=test()
    print(a)
    ---过程:就是没有返回值的函数
    总结:
    # 返回值数=0,返回None
    # 返回值数=1,返回object(对象原本是什么,返回什么)
    # 返回值数>1,返回tuple
    def test01():
        msg = 'test01'
        print(msg)
    
    
    def test02():
        msg = 'test02'
        print(msg)
        return msg
    
    def test03():
        msg = 'test03'
        print(msg)
        return 1,2,3,4,'a',['alex'],{'name':'alex'},None
    
    def test04():
        msg = 'test03'
        print(msg)
        return {'name':'alex'}
    t1=test01()
    t2=test02()
    t3=test03()
    t4=test04()
    print(t1)
    print(t2)
    print(t3)
    print(t4)
    # test01
    # test02
    # test03
    # test03
    # None
    # test02
    # (1, 2, 3, 4, 'a', ['alex'], {'name': 'alex'}, None)
    # {'name': 'alex'}
    ---函数参数
    # 1.形参变量只有在被调用时才分配内存单元,调用结束时,即刻释放所分配的内存单元,(相当于门牌号)
    # 因此,形参只在函数内部有效,函数,函数调用结束返回主调用函数后则不能再使用该形参变量
    # 2.实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,他们都必须有确定的值,(基本数据类型)
    # 以便把这些值传送给形参,因此应预先用赋值,输入的方法使参数获得确定值
    def calc(x,y): #x=2,y=3
        res=x**y
        return x
        return y
    res=calc(2,3) # 实参:常量
    # print(x)
    # print(y)
    print(res)
    # a=10
    # b=10
    # calc(a,b)  # 实参:变量
    
    
    def test(x,y,z):#x=1,y=2,z=3
        print(x)
        print(y)
        print(z)
    #3.位置参数,必须一一对应,缺一不行多一也不行
    # test(1,2,3)

    #4.关键字参数,无须一一对应,缺一不行多一也不行
    # test(y=1,x=3,z=4)

    #位置参数必须在关键字参数左边
    # test(1,y=2,3)#报错
    # test(1,3,y=2)#报错
    # test(1,3,z=2)
    # test(1,3,z=2,y=4)#报错
    # test(z=2,1,3)#报错
    # type='mysql' 默认参数
    # def handle(x,type='mysql'):
    # print(x)
    # print(type)
    # handle('hello')
    # handle('hello',type='sqlite')
    # handle('hello','sqlite')

    # def install(func1=False,func2=True,func3=True):
    # pass
    #参数组:**字典 *列表
    def test(x,*args):
        print(x)
        print(args)
    
    
    test(1)
    # 1
    # ()
    test(1,2,3,4,5)
    # 1
    # (2, 3, 4, 5)
    test(1,{'name':'alex'})
    # 1
    # ({'name': 'alex'},)
    test(1,['x','y','z'])
    # 1
    # (['x', 'y', 'z'],)
    test(1,*['x','y','z'])
    # 1
    # ('x', 'y', 'z')
    test(1,*('x','y','z'))
    # 1
    # ('x', 'y', 'z')
    
    # def test(x,**kwargs):
    #     print(x)
    #     print(kwargs)
    # test(1,y=2,z=3)
    # test(1,1,2,2,2,2,2,y=2,z=3)
    # test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值
    
    def test(x,*args,**kwargs):
        print(x)
        print(args,args[-1])
        print(kwargs,kwargs.get('y'))
    # test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
    test(1,1,2,1,1,11,1,y=2,z=3)
    # 1
    # (1, 2, 1, 1, 11, 1) 1
    # {'y': 2, 'z': 3} 2
    
    # test(1,*[1,2,3],**{'y':1})
  • 相关阅读:
    使用Python创建自己的Instagram滤镜
    TensorFlow v2.0实现逻辑斯谛回归
    自动驾驶研究回顾:CVPR 2019摘要
    dp cf 20190613
    简单搜索 kuangbin C D
    树形dp compare E
    区间dp E
    Codeforces Round #564 (Div. 2)
    网络流 + 欧拉回路 = B
    网络流 A
  • 原文地址:https://www.cnblogs.com/mada1027/p/11693451.html
Copyright © 2020-2023  润新知