104cz

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 一、函数定义&特性&返回值

1、函数定义

  函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可。

2、函数特性

  1)减少重复代码

  2)使程序编的可扩展

  3)使程序变得易于维护

 1 import time
 2 
 3 def test1():
 4     time_format = '%Y-%m-%d %X'
 5     time_current = time.strftime(time_format)
 6     print(time_current)
 7     return 0
 8 
 9 test1()
10 x= test1()
11 print(x)

3、函数返回值

#return:函数执行结果,为后续执行程序提供判断依据。

  #无:返回None

  #返回一个值,一个对象

  #返回多个值,包装成元组

 1 def test1():
 2     print('in the test1')
 3 
 4 def test2():
 5     print('in the test2')
 6     return 0
 7 
 8 def test3():
 9     print('in the test3')
10     return 1,[1,2],{'a':'b'}
11 
12 def test4():
13     print('in the test4')
14     return test2
15 
16 x = test1()
17 y = test2()
18 z = test3()
19 #返回test2函数
20 v = test4()
21 #调用
22 v1 = v()
23 
24 print(x,type(x))
25 print(y,type(y))
26 print(z,type(z))
27 print(v,type(v))
28 print(v1,type(v1))

二、函数参数

#函数参数
  #形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。
      # 因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量。
  #实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,
      # 以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值。

1、简单参数

 1 print('-'.center(50,'-'))
 2 def test1(x,y,z):
 3     'aaaaa'   #函数说明
 4     print(x,y,z)
 5 
 6 test1(1,3,5)       #位置参数调用:形参-实参一一对应。
 7 test1(y=3,x=1,z=5)   #关键字调用:与形参顺序无关
 8 #test1(y=3,1,5)     #报错
 9 #test1(3,z=1,5)     #报错
10 test1(1,z=5,y=3)     #混合调用:关键参数必须在位置参数后面

2、默认参数

 1 print('默认参数函数'.center(50,'-'))
 2 #默认参数:默认参数可以多个,但必须在非默认参数后。
 3     #特点:函数调用时,默认参数非必须传递
 4     #用途:自己想
 5 def test2(x,y,z=5,v=6):
 6     'aaaaa'
 7     print(x,y,z,v)
 8 
 9 test2(1,2)
10 test2(1,2,v=7)

3、参数组

 1 #参数组作用:为后期函数拓展做准备
 2 print('不固长参数函数(参数组):元组形式'.center(50,'-'))
 3 def test3(x,y=2,*args):
 4     print(x,y)
 5     print(args)
 6 #接收位置参数作,转化为元组
 7 test3(1,2,3,4,5)
 8 #test3(1,y=10,3,4,5)    #关键字参数必须在位置参数后
 9 #test3(1,3,4,5,y=10)    #3被当做默认参数传递,y=10,再次传递,出错
10 test3(1,7,*(8,9))  #test3(1,*[7,8,9]) ,test3(1,*{7:1,8:2,9:3})  #转化为元组
11 
12 print('不固长参数函数(参数组):字典形式'.center(50,'-'))
13 def test4(x,y=2,**kwargs):
14     print(x,y)
15     print(kwargs)
16 
17 #接收关键字参数作,转化为元组
18 test4(1,3,name='a',age=8)
19 test4(1,3,**{'name':'a','age':8})
20 test4(1,name='a',age=8,y=10)

4、各类型参数

 1 #参数顺序:1)普通参数、默认参数、元组参数组、字典参数组,建议使用这种。
 2 #           2)普通参数、元组参数组、默认参数、字典参数组
 3 print('各类型参数'.center(50,'-'))
 4 def test5(x,y=2,*args,**kwargs):
 5 #def test5(x,*args,y=2,**kwargs):
 6     print(x,y)
 7     print(args)
 8     print(kwargs)
 9 
10 test5(1,3,4,5,6,name='a',age=8)
11 test5(1,name='a',age=8,y=10)    #除非元组参数组不定义值,否则只能按顺序赋值
12 test5(name='a',age=8,y=10,x=11)
13 #建议按照顺序,别搞那么多幺蛾子。。。。。

 

三、局部变量&全局变量

 1 #全局与局部变量
 2     # 在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量。
 3     # 全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序。
 4     # 当全局变量与局部变量同名时:
 5         # 在定义局部变量的子程序内,局部变量起作用;在其它地方全局变量起作用。
 6 
 7 school = 'gd'
 8 sch_glo = 'gx'
 9 
10 def charge_name(name):
11     print('before name: ',name)
12     name = name.upper()
13     print('after name: ',name)
14     school = 'gddddddd'
15     print('fnc school:',school)
16     global sch_glo                      #申明为全局变量:不要这么干
17     sch_glo = 'gxxxxxxx'
18     print('fnc sch_glo:',sch_glo)
19     global name_glo
20     name_glo = 'czzzzzzzzzz'            #定义全局变量:永远永远永远不要这么干
21     print('fnc name_glo:',name_glo)
22 
23 name = 'cz'
24 print(name)
25 charge_name(name)
26 print(name)
27 print(school)
28 print(sch_glo)
29 print(name_glo)
30 
31 
32 print(''.center(50,'-'))
33 list_name = ['aa','bb','cc']
34 
35 def charge_name2(list):
36     list[0] = 'aaaaaa'
37     print('in fnc: ',list)
38 
39 charge_name2(list_name)
40 print(list_name)
41 #what the fuck...,被修改了。why?引用传递问题。

 

 

四、递归函数

 1 #在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
 2 #递归特性:
 3     # 1. 必须有一个明确的结束条件
 4     # 2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少
 5     # 3. 递归效率不高,递归层次过多会导致栈溢出
 6     #     (在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,
 7     #       每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出)
 8 
 9 
10 def n_factorial(n):
11     print(n,'*',end='')
12     if n-1 >0:
13         return n*n_factorial(n-1)
14     else:
15         return n
16 
17 
18 a = n_factorial(5)
19 print('')
20 print(a)

 

五、高阶函数

 1 #高阶函数:
 2     #1、把一个函数当做实参传递给另外一个函数
 3     #2、返回值是一个函数
 4 
 5 def add(x,y,f):
 6     return f(x)+f(y)
 7 
 8 res = add(3,-6,abs)
 9 
10 print(res)

六、匿名函数

 1 def test(n):
 2     print(n)
 3 
 4 test(3)
 5 
 6 #用完就删除,改匿名函数
 7 #只能处理简单数据统计或者三元运算符
 8 #https://blog.csdn.net/zjuxsl/article/details/79437563
 9 (lambda n:print(n)) (5)
10 
11 calc = lambda n:print(n)
12 
13 calc(5)
14 
15 # lambda argument_list: expression
16     #argument_list:参数列表
17     #expression:关于参数的表达式。表达式中出现的参数需要在argument_list中有定义,并且表达式只能是单行的。
18 
19 #三个特性
20     #lambda函数是匿名的:所谓匿名函数,通俗地说就是没有名字的函数。lambda函数没有名字。
21     #lambda函数有输入和输出:输入是传入到参数列表argument_list的值,输出是根据表达式expression计算得到的值。
22     #lambda函数一般功能简单:单行expression决定了lambda函数不可能完成复杂的逻辑,只能完成非常简单的功能。
23         # 由于其实现的功能一目了然,甚至不需要专门的名字来说明。

七、内置函数

详见:https://docs.python.org/3/library/functions.html?highlight=built

 

posted on 2018-09-16 10:09  104cz  阅读(153)  评论(0)    收藏  举报