函数返回值为数组
def fav():
return ("lanqiu","zuqiu")
allen = fav()
print(allen)
print("=============")
x,y= fav()
print(x,y)
默认参数 位置参数
def host(host,port=80):
print("%s:%s"%(host,port))
host("127.0.0.1","8080")
host("8080","127.0.0.1")
host("127.0.0.1") # 默认参数
host(port = "8080",host = "127.0.0.1") # 位置参数
不定长参数1-元组
def foo(name,*info):
print(name)
print(info)
foo("maotai",11,22,33,44)
不定长参数2-元组,字典 直接传参
def foo(name,*args,**kwargs):
print(name)
print(args)
print(kwargs)
foo("maotai",11,22,33,44,gender="male")
maotai
(11, 22, 33, 44)
{'gender': 'male'}
不定长参数3-3种传参方式
def foo(x,y, *args, **kwargs):
print(name)
print(args)
print(kwargs)
#
foo("maotai", 11, 22, 33, 44, gender="male")
#
foo("maotai", *(11, 22, 33, 44), **{"gender": "male"})
#
atuple = (1, 2, 3, 4)
adict = {"gender": "male"}
foo("maotai", *atuple, **adict)
匿名函数
lambda arg:表达式
1.求和
add2=lambda x,y=20:x+y
def add(x,y=20): return x+y
def add(x,y=20): return x+y
s = add(10,20)
print(s)
add2=lambda x,y=20:x+y
s2=add2(100,200)
print(s2)
s2=add2(100)
print(s2)
2,显示数组
def showAllTuple(*z):return z
g=lambda *z:z
print(g(1,2,3,4))
简单的闭包
def counter(start_at=0):
count = [start_at]
def incr():
count[0] += 1
return count[0]
return incr
count = counter(10)
print(count())
print(count())
print(count())
print(count())