Python学习笔记_1_基础_7:函数

 

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

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?引用传递问题。

 

猜你喜欢

转载自www.cnblogs.com/104cz/p/9655210.html
今日推荐