码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-14 22:34:02      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:赋值   有关   参数调用   函数参数   int   none   形参和实参   lex   test   

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)#关键字参数不能在位置参数前面

 

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

标签:赋值   有关   参数调用   函数参数   int   none   形参和实参   lex   test   

原文地址:http://www.cnblogs.com/xiaobai005/p/7834730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!