一、函数返回值

函数返回值如果是多个字符串、数字、列表、字典,则返回一个元组,如下

def test1():
    pass


def test2():
    #print("hello test2")
    return 0

def test3():
    #print('hello test3')
    return 1,10,['aaa',111],{1:'www'}
x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)
返回结果>>>>
None
0
(1, 10, ['aaa', 111], {1: 'www'})

函数参数位置与传值:

def test4(x,y):
    print(x)
    print(y)
def test5(x,y,z):
    print(x)
    print(y)
    print(z)
test4(y=1,x=2)
#test4(2,y=1)#这样也是可以的
test5(3,z=1,y=2)
返回结果>>>
2
1
3
2
1

 

 

二、函数参数

def test1(*args):#接受n个位置参数,转换为元组输出
    print(args)
test1(1,2,3,4)
def test4(x,*args):#将实参的第一个值赋给x,其余值赋给args,转换为元组输出
    print(x)
    print(args)
test4(1,2233,4,5)
def test2(**kwargs):
    print(kwargs)
test2(name='yuqing',age='27')



def test3(name,*args,**kwargs):#把n个关键字参数,转换为字典
    print(name)
    print(args)
    print(kwargs)
test3('yyuqing',1,2,sex='m',age=27)
返回结果>>>
(1, 2, 3, 4)
1
(2233, 4, 5)
{'age': '27', 'name': 'yuqing'}
yyuqing
(1, 2)
{'age': 27, 'sex': 'm'}

 

posted on 2018-04-27 11:50  哎呀墨儿  阅读(174)  评论(0编辑  收藏  举报