Day4 python基础4 函数
一、函数基本语法
利用函数可以减少重复代码,提高程序的可扩展性,使程序易于维护。
基本语法
1 def fucn1(): #定义函数 2 print("in the func1") 3 return 0 4 5 x = fucn1() #调用函数
二、返回值
1.需要得到函数的执行结果,就要用return语句返回结果
2.一但遇到return语句就会跳出本函数
3.若未指定return,则返回值为None
返回值的几种形式:
1 def test1(): #不指定return,则返回值为None 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 test3 #返回函数在内存中的地址 11 12 def test4(): 13 print('in the test3') 14 return 1,'hello',['alex','xiaoz'],{'sdf','bbb'} #个数不固定,类型不固定,将所有返回值组成一个元组 15 16 x = test1() 17 y = test2() 18 z = test3() 19 u = test4() 20 21 print(x,y,z,u)
1 in the test1 2 in the test2 3 in the test3 4 in the test3 5 None 0 <function test3 at 0x000001DD8FECD510> (1, 'hello', ['alex', 'xiaoz'], {'bbb', 'sdf'})
三、函数参数
形参 形参只在函数内部有效。
实参 在执行函数调用时,实参必须有确定的值,然后将值传递给形参。
1 def test(x): #x为形参 2 print(x) 3 a = 1 4 test(a) #a为实参
位置参数和关键字参数,注意关键字参数必须写在位置参数之后。
1 def test(x,y): 2 print(x) 3 print(y) 4 5 test(1,2) #位置参数调用,一一对应,多不行,少不行 6 test(y=2,x=1) #关键字参数调用 7 8 test(3,y=4) #关键字参数必须写在位置参数之后 9 #test(y=5,6) 错误写法
默认参数
1 #默认参数:调用函数时,默认参数非必需传递 2 def test(x,y=2): 3 print(x) 4 print(y) 5 6 test(1) 7 8 test(3,4)
1 2 3 4
参数组
若函数在定义时不确定用户想传入多少个参数,可以使用参数组。
1. *args 接收N个位置参数,转换成元组的形式
1 def test(*args): 2 print(args) 3 4 test(2,1,5,6,8,5) 5 test(*[1,2,3,4,5]) # *args=*[1,2,3,4,5] args=tuple([1,2,3,4,5])
(2, 1, 5, 6, 8, 5)
(1, 2, 3, 4, 5)
1 def test1(x,*args): 2 print(x) 3 print(args) 4 5 test1(1,2,3,4,7,8)
1
(2, 3, 4, 7, 8)
2. **kwargs 接收N个关键字参数,转换成字典的形式
1 def test2(**kwargs): 2 print(kwargs) 3 print(kwargs['name']) 4 5 test2(name='ztian',age=8,sex='F') 6 test2(**{'name':"ztian",'age':8})
{'name': 'ztian', 'age': 8, 'sex': 'F'} ztian {'name': 'ztian', 'age': 8} ztian
1 def test3(name,**kwargs): 2 print(name) 3 print(kwargs) 4 5 test3('ztian',age=8,sex='f')
ztian {'age': 8, 'sex': 'f'}
几种参数设置:
def test4(name,age=18,**kwargs): print(name) print(age) print(kwargs) test4('ztian',sex='f',hobby='bmw',age=3)
ztian 3 {'sex': 'f', 'hobby': 'bmw'}
def test5(name,age=18,*args,**kwargs): print(name) print(age) print(args) print(kwargs) test5('ztian',age=3,sex='f',hobby='bmw')
ztian 3 () {'sex': 'f', 'hobby': 'bmw'}
四、局部变量与全局变量
在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量。
全局变量的作用域是整个程序,局部变量的作用域是定义该变量的函数。
当全局变量与局部变量同名时,在定义局部变量的函数内,局部变量起作用,在其他地方全局变量起作用。
1 def change_name(name): 2 print(name) 3 name = 'Ztian' # 这里的name是局部变量,这个函数就是这个变量的作用域 4 print(name) 5 6 name = "ztian" 7 change_name(name) 8 print(name)
ztian
Ztian
ztian
利用局部变量强行修改全局变量——global
1 name = 'ZTIAN' 2 def chang_name(): 3 global name #篡位 4 name = 'ztian' 5 6 chang_name() 7 print(name)
ztian
注意
1 sex = 'f' #全局变量 2 names = ['laowang','laoli'] 3 4 def change_name(name): 5 sex = 'm' 6 names[0] = 'laozhang' 7 print(name,sex) 8 9 name = 'ZTIAN' # name是局部变量,这个函数就是这个变量的作用域 10 print(name,sex,names) 11 12 name = "ztian" 13 change_name(name) 14 print(name,sex,names) # 整数,字符串改不了;列表,集合,字典可以改;元组本身就无法修改
ztian m ZTIAN m ['laozhang', 'laoli'] ztian f ['laozhang', 'laoli']
五、递归
在函数内部调用函数自己。
递归:1.必须有一个明确的结束条件;
2.每次进行更深一层的递归时,问题规模比之上一次应有所减小;
3.递归的效率不高,递归层数过多会导致栈溢出,python中最多递归999层。
1 def calc(n): 2 print(n) 3 if int(n/2)>0: 4 return calc(int(n/2)) 5 print("->",n) 6 7 calc(10)
10 5 2 1 -> 1
六、高阶函数
一个函数接收另一个函数作为参数,这种函数就称为高阶函数。
1 def add(a,b,f): 2 return f(a)+f(b) 3 res = add(3,-6,abs) 4 print(res)
9