一、函数定义
1.函数的定义
def test(x):
"The function definitions"
x += 1
return x
print(test(5))
#def:定义函数的关键字
#test:函数名
#():定义形参
#"..."描述函数功能
#x += 1:代码块
#return:返回值
2.位置参数
注:赋的值必须和形参一一对应,不能多也不能少。
def cal(x,y,z):
print(x)
print(y)
print(z)
return x
cal(2,5,6)
3.关键字参数
注:赋的值可以调换顺序,数量也是不能多且不能少。
def cal(x,y,z):
print(x)
print(y)
print(z)
return x
cal(z = 6, x = 2, y = 5)
4.位置参数和关键字参数同时存在
注:位置参数必须在关键字参数左边。
def cal(x,y,z):
print(x)
print(y)
print(z)
return x
cal(2, z = 6, y = 5)
5.已经定义了值的参数
def handle(x,type='mysql'):
print(x)
print(type)
handle('hello')
# 返回:
hello
mysql
handle('hello',type='sqlite')
# 返回:
hello
sqlite
handle('hello','sqlite')
# 返回:
hello
sqlite
6.参数组
注:*args数组,**kwargs字典。
def test(x,*args):
print(x)
print(arg)
test(1,2,3,4,5,6)
# 返回:
1
(2, 3, 4, 5, 6)
def test(x,*args):
print(x)
print(arg)
test(1,*['th','handsome',666])
# 返回:('th', 'handsome', 666)
def test(x,**kwargs):
print(x)
print(kwargs)
test(1,**{'name':'th'})
# 返回:
1
{'name': 'th'}
二、全局变量和局部变量
1.无global关键字(有声明局部变量)
idea = '我要涨工资'
def th():
print(idea) #输出全局变量的值
def boss():
idea = 'OK' #定义局部变量
print(idea) #输出全局变量的值
th()
boss()
返回值:
我要涨工资
OK
2.无global关键字(无声明局部变量)
注:如果全局变量是可变类型,可以对全局变量的内部元素进行操作,但无法对全局变量重新赋值。
idea = ['我要涨工资','我要放长假']
def th():
print(idea)
def boss():
idea[0] = '工资翻倍'
idea.append('带薪年假')
print(idea)
th()
boss()
返回值:
['我要涨工资', '我要放长假']
['工资翻倍', '我要放长假', '带薪年假']
3.有global关键字(有声明局部变量)
注:优先调用局部变量;声明global,可以直接对全局变量重新赋值。
idea = ['呵呵']
def th():
idea = ['我要涨工资','我要放长假']
print(idea)
def boss():
global idea
idea = 'OK'
print(idea)
th()
boss()
print(idea)
返回值:
['我要涨工资', '我要放长假']
OK
OK
4.有global关键字(无声明局部变量)
注:无声明局部变量时,调用全局变量。
idea = ['我要涨工资','我要放长假']
def th():
print(idea)
def boss():
global idea
idea = 'OK'
print(idea)
th()
boss()
print(idea)
返回值:
['我要涨工资', '我要放长假']
OK
OK
5.nonlocal关键字(指定上一级变量)
idea = 'emmmmmm'
def th():
idea = '我要涨工资'
def boss():
nonlocal idea
idea = '给员工加薪'
boss()
print(idea)
print(idea)
th()
print(idea)
返回值:
emmmmmm
给员工加薪
emmmmmm
三、递归
import time
people_list = ['马云', '马化腾', '李彦宏', 'th','赵薇']
def ask_way(people_list):
print('*' * 60)
if len(people_list) == 0:
return '不知道路'
people = people_list.pop(0)
if people == 'th':
return "%s说:重邮在重庆市南岸区崇文路2号,坐346公交车就能到。"%people
print('记者问:%s您好,您知道重邮在哪吗?' % people)
print('%s回答:我不知道,我帮您问问%s吧' % (people, people_list))
time.sleep(1)
res = ask_way(people_list)
print('%s问的结果是:%s'%(people,res))
return res
res = ask_way(people_list)
print(res)
四、map函数
1.map函数的功能
map()接收一个函数 def和一个或多个 list,将 def依次作用在 list的每个元素,得到一个新的list。
2.用普通函数实现 map函数功能
num = [6,8,9]
def add_one(x):
return x+1
def map_test(func, array):
ret = []
for i in array:
res = func(i)
ret.append(res)
return ret
res = map_test(add_one, num)
print(res)
# 返回:[7, 9, 10]
3.直接使用内置 map函数
注:map返回的结果是迭代器,需要先转换成 list再打印。
num = [6,8,9]
res = list(map(lambda x:x+1,num))
print(res)
# 返回:[7, 9, 10]
五、filter函数
1.filter函数的功能
filter()函数接收一个函数 def和一个 list,这个函数 def的作用是对每个元素进行判断,返回 True或 False,filter()根据判断
结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新 list。
2.用普通函数实现 filter函数功能
l = ['th','mayun','mahuateng','malaoshi','mamaipi']
def delete(n):
return n.startswith('ma')
def filter_test(func, array):
ret = []
for i in array:
if not func(i):
ret.append(i)
return ret
res = filter_test(delete, l)
print(res)
# 返回:['th']
3.直接使用内置 filter函数
l = ['th','mayun','mahuateng','malaoshi','mamaipi']
res = list(filter(lambda x:not x.startswith('ma'), l))
print(res)
# 返回:['th']
六、reduce函数
1.reduce函数的功能:
对一个序列进行处理,然后将该序列进行合并操作。
2.用普通函数实现 reduce函数功能
l = [1,3,5,]
def muti(x, y):
return x+y
def reduce_test(func, array, init = None):
if init is None:
res = array[0]
else:
res = init
for i in array:
res = func(res, i)
return res
res = reduce_test(muti, l, 100)
print(res)
# 返回:109
3.直接使用内置 reduce函数
注:python3中使用 reduce函数需要从functools模块中调用。
l = [1,3,5,]
from functools import reduce #调用
print(reduce(muti, l, 3))
# 返回:109