day3-3.5函数与函数式编程

# Author :Gao ling
"""
#函数
def func1():

print('in the func1')
return 0

#过程 过程是没有返回值的函数
def func2():
'''testing2'''
print('in the func2')

x=func1()
y=func2()

print('from func1 return is %s' %x)
print('from func1 return is %s' %y)
"""

def test1():
print('in the test1')
return 0

x=test1()
print(x)
############################################
# Author :Gao ling
"""
def test1():
print('in the test1')
return 0

x=test1()
print(x)
"""
#return可以任意类型,任意个数
def test1():
print('in the test1')

def test2():
print('in the test2')
return 0

def test3():
print('in the test2')
return 1,"hello",['alex','wupeiqi'],{'name':'alex'}
x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)
"""
总结:
返回值数=0:返回None
返回值数=1:返回object
返回值数>1:返回tuple
"""
############################################################
'''
def test(x,y):#xy为形参 不占用空间
print(x)
print(y)

#test(1,2)#实参 实际存在,占用空间 实参和形参一一对应 位置参数调用与形参一一对应
#test(y=1,x=2)#与形参顺序无关 关键参数
#a=1
#b=2
#test(x=a,y=b)#关键字参数
#test(x=2,3)#报错
#test(3,x=3)#既有位置的调用,又有关键字调用,按着位置调用来
#test(3,y=2)#不报错 本身满足一一对应关系,关键参数不能写在位置参数前
'''


def test(x,y,z):#xy为形参 不占用空间
print(x)
print(y)
print(z)
#test(3,y=2,1)#报错 关键参数不能写在位置参数前
test(3,2,z=1)#不报错
#########################################################
"""
def test(x,y=2):#
print(x)
print(y)
test(1)
"""
#默认参数特点:调用函数的时候,默认参数可以有可以无,默认参数非必须传递
#用途:1.默认安装值 2.连接数据端口号

def conn(host,post=3306):
pass
conn(2)
############################################################
# Author :Gao ling
#*args:接受N个位置参数,转换成元组的方式
#**kwargs 接受N个关键字参数,转化为字典的方式
#参数有位置参数,关键字参数,默认参数
"""
def test(x,y,z=2):#z=2为默认参数
print(x)
print(y)
test(1,2)#位置参数
"""

"""
#参数组*+变量名
#参数组:*args:将位置参数变为元组;**kwargs:把N个关键字参数转换成式字典的方式
def test(*args):
print(args):将参数变为

test(1,2,3,4,5,5)
test(*[1,2,3,4,5])#*args=*[1,2,3,4,5] 会变成元组args=tuple([1,2,3,4,5])
"""

"""
#元组
def test1(x,*args):
print(x)
print(args)
test1(1,2,3,4,5,6,7)
"""

"""
#字典 **kwargs:把N个关键字参数转换成式字典的方式
def test2(**kwargs):
print(kwargs)
print(kwargs['name'])
print(kwargs['age'])
print(kwargs['sex'])

test2(name='ales',age=8,sex='F')
test2(**{'name':'ales','age':8,'sex':'F'})
"""

"""
def test3(name,**kwargs):
print(name)
print(kwargs)
test3('alex',age=18,sex='m')
"""

def test3(name,age=18,**kwargs):#位置参数,默认参数,参数组.参数组一定要放后边
print(name)
print(age)
print(kwargs)
test3('alex',sex='m',hobby='tesla')#age=18可以省略不写
test3('alex',3,sex='m',hobby='tesla')
test3('alex',age=3,sex='m',hobby='tesla')#默认参数赋值可以以关键字
###########################################################################






posted @ 2018-01-13 20:19  灵儿三石  阅读(111)  评论(0编辑  收藏  举报