python之函数
函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ''' 2 y=2*x+1 3 x=3 4 y->7 5 x=3 6 y->7 7 ''' 8 # def test(x): 9 # ''' 10 # 2*x+1 11 # :param x:整形数字 12 # :return: 返回计算结果 13 # ''' 14 # y=2*x+1 15 # return y 16 # 17 # def test(): 18 # ''' 19 # 2*x+1 20 # :param x:整形数字 21 # :return: 返回计算结果 22 # ''' 23 # x=3 24 # y=2*x+1 25 # return y 26 # a=test() 27 # print(a) 28 29 #过程:就是没有返回值的函数 30 31 32 # def test01(): 33 # msg = 'test01' 34 # print(msg) 35 # 36 # 37 # def test02(): 38 # msg = 'test02' 39 # print(msg) 40 # return msg 41 # 42 # def test03(): 43 # msg = 'test03' 44 # print(msg) 45 # return 1,2,3,4,'a',['alex'],{'name':'alex'},None 46 # 47 # def test04(): 48 # msg = 'test03' 49 # print(msg) 50 # return {'name':'alex'} 51 # t1=test01() 52 # t2=test02() 53 # t3=test03() 54 # t4=test04() 55 # print(t1) 56 # print(t2) 57 # print(t3) 58 # print(t4) 59 60 61 62 63 64 65 66 # def calc(x,y): #x=2,y=3 67 # res=x**y 68 # return x 69 # return y 70 # res=calc(2,3) 71 # # print(x) 72 # # print(y) 73 # print(res) 74 # # a=10 75 # # b=10 76 # # calc(a,b) 77 78 79 # def test(x,y,z):#x=1,y=2,z=3 80 # print(x) 81 # print(y) 82 # print(z) 83 84 #位置参数,必须一一对应,缺一不行多一也不行 85 # test(1,2,3) 86 87 #关键字参数,无须一一对应,缺一不行多一也不行 88 # test(y=1,x=3,z=4) 89 90 #位置参数必须在关键字参数左边 91 # test(1,y=2,3)#报错 92 # test(1,3,y=2)#报错 93 # test(1,3,z=2) 94 # test(1,3,z=2,y=4)#报错 95 # test(z=2,1,3)#报错 96 97 # def handle(x,type='mysql'): 98 # print(x) 99 # print(type) 100 # handle('hello') 101 # handle('hello',type='sqlite') 102 # handle('hello','sqlite') 103 104 # def install(func1=False,func2=True,func3=True): 105 # pass 106 107 #参数组:**字典 *列表 108 # def test(x,*args): 109 # print(x) 110 # print(args) 111 112 113 # test(1) 114 # test(1,2,3,4,5) 115 # test(1,{'name':'alex'}) 116 # test(1,['x','y','z']) 117 # test(1,*['x','y','z']) 118 # test(1,*('x','y','z')) 119 120 # def test(x,**kwargs): 121 # print(x) 122 # print(kwargs) 123 # test(1,y=2,z=3) 124 # test(1,1,2,2,2,2,2,y=2,z=3) 125 # test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值 126 127 # def test(x,*args,**kwargs): 128 # print(x) 129 # print(args,args[-1]) 130 # print(kwargs,kwargs.get('y')) 131 # test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错 132 # test(1,1,2,1,1,11,1,y=2,z=3) 133 134 # test(1,*[1,2,3],**{'y':1})
引用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # def bar(): 2 # print('from bar') 3 # def foo(): 4 # print('from foo') 5 # bar() 6 # 7 # foo() 8 9 10 11 # def foo(): 12 # print('from foo') 13 # bar() 14 # def bar(): 15 # print('from bar') 16 # 17 # foo()
多层调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 name='海风' 2 def huangwei(): 3 name = "黄伟" 4 print(name) 5 def liuyang(): 6 name = "刘洋" 7 print(name) 8 def nulige(): 9 name = '炉指花' 10 print(name) 11 print(name) 12 nulige() 13 liuyang() 14 print(name) 15 16 print(name) 17 huangwei() 18 print(name)
递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #递归 2 # def calc(n): 3 # print(n) 4 # if int(n / 2) == 0: 5 # return n 6 # res=calc(int(n / 2)) 7 # return res 8 # 9 # 10 # res=calc(10) 11 # print(res)
匿名:lambda
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # lambda x:x+1 2 3 4 # def calc(x): 5 # return x+1 6 # 7 # res=calc(10) 8 # print(res) 9 # print(calc) 10 11 # print(lambda x:x+1) 12 # func=lambda x:x+1 13 # print(func(10)) 14 15 # name='alex' #name='alex_sb' 16 # def change_name(x): 17 # return name+'_sb' 18 # 19 # res=change_name(name) 20 # print(res) 21 # 22 # func=lambda x:x+'_sb' 23 # res=func('ds') 24 # print('匿名函数的运行结果',res) 25 26 27 # func=lambda x,y,z:x+y+z 28 # print(func(1,2,3)) 29 30 31 # name1='alex' 32 # name2='sbalex' 33 # name1='supersbalex' 34 35 36 37 # def test(x,y,z): 38 # return x+1,y+1 #----->(x+1,y+1) 39 # 40 # lambda x,y,z:(x+1,y+1,z+1)
map函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 num_l=[1,2,10,5,3,7] 2 num1_l=[1,2,10,5,3,7] 3 4 ret=[] 5 # for i in num_l: 6 # ret.append(i**2) 7 # 8 # print(ret) 9 10 # def map_test(array): 11 # ret=[] 12 # for i in num_l: 13 # ret.append(i**2) 14 # return ret 15 16 # ret=map_test(num_l) 17 # rett=map_test(num1_l) 18 # print(ret) 19 # print(rett) 20 21 # num_l=[1,2,10,5,3,7] 22 # # #lambda x:x+1 23 # def add_one(x): 24 # return x+1 25 # # #lambda x:x-1 26 # def reduce_one(x): 27 # return x-1 28 # 29 # #lambda x:x**2 30 # def pf(x): 31 # return x**2 32 # 33 # def map_test(func,array): 34 # ret=[] 35 # for i in num_l: 36 # res=func(i) #add_one(i) 37 # ret.append(res) 38 # return ret 39 # print(map_test(add_one,num_l)) 40 # print(map_test(lambda x:x+1,num_l)) 41 42 43 # print(map_test(reduce_one,num_l)) 44 # print(map_test(lambda x:x-1,num_l)) 45 46 # print(map_test(pf,num_l)) 47 # print(map_test(lambda x:x**2,num_l)) 48 49 50 51 #终极版本 52 def map_test(func,array): #func=lambda x:x+1 arrary=[1,2,10,5,3,7] 53 ret=[] 54 for i in array: 55 res=func(i) #add_one(i) 56 ret.append(res) 57 return ret 58 59 # print(map_test(lambda x:x+1,num_l)) 60 res=map(lambda x:x+1,num_l) 61 # print('内置函数map,处理结果',res) 62 # for i in res: 63 # print(i) 64 # print(list(res)) 65 # print('传的是有名函数',list(map(reduce_one,num_l))) 66 # 67 # 68 # msg='linhaifeng' 69 # print(list(map(lambda x:x.upper(),msg)))
filter函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao'] 2 # 3 # 4 # 5 # 6 # def filter_test(array): 7 # ret=[] 8 # for p in array: 9 # if not p.startswith('sb'): 10 # ret.append(p) 11 # return ret 12 # 13 # res=filter_test(movie_people) 14 # print(res) 15 16 17 # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] 18 # def sb_show(n): 19 # return n.endswith('sb') 20 # 21 # def filter_test(func,array): 22 # ret=[] 23 # for p in array: 24 # if not func(p): 25 # ret.append(p) 26 # return ret 27 # 28 # res=filter_test(sb_show,movie_people) 29 # print(res) 30 31 #终极版本 32 # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] 33 # def sb_show(n): 34 # return n.endswith('sb') 35 # # --->lambda n:n.endswith('sb') 36 # 37 # def filter_test(func,array): 38 # ret=[] 39 # for p in array: 40 # if not func(p): 41 # ret.append(p) 42 # return ret 43 # 44 # res=filter_test(lambda n:n.endswith('sb'),movie_people) 45 # print(res) 46 # 47 # #filter函数 48 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] 49 # print(filter(lambda n:not n.endswith('sb'),movie_people)) 50 # 51 # 52 # 53 # res=filter(lambda n:not n.endswith('sb'),movie_people) 54 # print(list(res)) 55 # 56 # 57 # print(list(filter(lambda n:not n.endswith('sb'),movie_people))) 58 59 print(list(filter(lambda n:not n.endswith('sb'),movie_people)))
内置函数:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # print(abs(-1)) 2 # print(abs(1)) 3 # 4 # print(all([1,2,'1'])) 5 # print(all([1,2,'1',''])) 6 # print(all('')) 7 8 # print(any([0,''])) 9 # print(any([0,'',1])) 10 11 12 # print(bin(3)) 13 14 #空,None,0的布尔值为False,其余都为True 15 # print(bool('')) 16 # print(bool(None)) 17 # print(bool(0)) 18 19 20 name='你好' 21 # print(bytes(name,encoding='utf-8')) 22 # print(bytes(name,encoding='utf-8').decode('utf-8')) 23 # 24 # print(bytes(name,encoding='gbk')) 25 # print(bytes(name,encoding='gbk').decode('gbk')) 26 # 27 # print(bytes(name,encoding='ascii'))#ascii不能编码中文 28 29 # print(chr(46)) 30 31 # print(dir(dict)) 32 33 # print(divmod(10,3)) 34 35 # dic={'name':'alex'} 36 # dic_str=str(dic) 37 # print(dic_str,type(dic_str)) 38 39 #可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型 40 # print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf')) 41 # print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23')) 42 # 43 # name='alex' 44 # print(hash(name)) 45 # print(hash(name)) 46 # 47 # 48 # print('--->before',hash(name)) 49 # name='sb' 50 # print('=-=>after',hash(name)) 51 52 53 # print(help(all)) 54 55 # print(bin(10))#10进制->2进制 56 # print(hex(12))#10进制->16进制 57 # print(oct(12))#10进制->8进制 58 59 60 61 # print(isinstance(1,int)) 62 # print(isinstance('abc',str)) 63 # print(isinstance([],list)) 64 # print(isinstance({},dict)) 65 # print(isinstance({1,2},set)) 66 67 # name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈' 68 # print(globals()) 69 # print(__file__) 70 # 71 # def test(): 72 # age='1111111111111111111111111111111111111111111111111111111111111' 73 # # print(globals()) 74 # print(locals()) 75 # 76 # test() 77 78 # l=[1,3,100,-1,2] 79 # print(max(l)) 80 # print(min(l))