函数

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。

函数能提高应用的模块性,和代码的重复利用率。

函数为什么要有返回值:为了方便后边的程序根据返回结果判断如何执行。

def test1():

    print('hello')

调用方法:

x=test1()

print(x)

结果是:hello

def test2(x,y):

    print(x,y)

函数参数位置顺序:位置参数(需要一一对应)--->关键字参数--->默认参数

def test3(x,y,z):

    print(x,y,z)

test3(1,y=2,z=3)

 

def test4(x,y,z=3)

    print(x,y,z)

 

def test5(x,y=2,*args,**kwargs)

    print(x,y,args,kwargs)

test5(4,y=5,'cat',name='grap_lee',age=18)

 

*args接收多个位置参数转换成元组形式。

**kargs接收多个关键字参数转换成字典。

 

posted @ 2017-06-19 18:59  grape_lee  阅读(84)  评论(0编辑  收藏  举报