• Python学习之路:函数参数及调用


    return:结束函数并返回值

    没有return时:返回None

    返回值数=1时:返回具体值

    返回值是数字+字符串+列表等:返回一个元组

    需要return是需要函数完整调用

    def test1():
        print('in the test1')
    
    def test2():
        print('in the test2')
        return 0 #结束函数并返回0
    
    def test3():
        print('in the test3')
        return 1,'hello',['alex','wupeiqi'], {'name','alex'}#结束函数并返回0
    
    x=test1() #return返回值可以赋值给变量
    y=test2()
    z=test3()
    print(x)
    print(y)
    print(z)
    

     函数参数:

    def test(x,y):
        print(x)
        print(y)
    
    test(1,2) #1传给x,2传给y;x,y叫形参(位置参数);1,2叫实参;形参和实参的位置一一对应;
    test(y=1,x=2)#关键字调用:与形参顺序无关
    test(1,2)#位置参数调用:与形参一一对应
    test(3,y=2)#既有位置参数调用又有关键字参数调用,按位置参数调用执行
    
    
    def test(x,y,z):
        print(x)
        print(y)
        print(z)
    test(3,z=2,y=6)
    test(3,y=2,6)#关键字参数不能在位置参数前面
    
  • 相关阅读:
    数学
    数学
    Computer Science
    数学
    Computer Science
    元学习
    数学
    数学
    数学
    数学
  • 原文地址:https://www.cnblogs.com/xiaobai005/p/7834730.html
Copyright © 2020-2023  润新知