1 Star 0 Fork 0

读梦人/python_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
015-函数 7.89 KB
一键复制 编辑 原始数据 按行查看 历史
15、函数
15.1 基本函数
1)例如读取文件函数 read_file
##函数结构:
###################################################测试_START###################################################
#声明函数def 函数名称(参数):
def read_file(file_path):
#with 上下文管理器打开文件,函数执行完自动关闭,不需手动close
with open(file_path,'r',encoding='utf-8') as f:
#f.read()读文件内容,赋值给res
res = f.read()
#return 返回值
return res
#调用read_file函数,变量当前路径 1.txt 文件
f1 = read_file('1.txt')
print(f1)
###################################################测试_END###################################################
2)多个参数,(*args,**kwatgs)
*args #tuple,任意多个无名参数
**kwargs #dict
###################################################测试_START###################################################
print('================================*args=======================')
###tuple,
def func_1(*args):
print(f'value:{args},type:{type(args)}')
func_1(1,2,3)
print('================================**kwargs=======================')
def func_2(**kwargs):
print(f'value:{kwargs},type:{type(kwargs)}')
#如果函数传参**kwargs,那么必须传入字典格式
func_2(a=123,b=234,c=345)
print('================================*args+**kwargs=======================')
def func_3(*args,**kwargs):
print(f'value:{args},type:{type(args)}')
print(f'value:{kwargs},type:{type(kwargs)}')
func_3(1,2,3,a=123,b=234,c=345)
###################################################测试_END###################################################
3)缺省参数介绍
def func_3(x=3):
print(x)
#比如直接调用func_3,会默认传参x=3,输出为3
func_3()
###################################################测试_START###################################################
In [26]: def test_print(name,age='未知',s_class='未知',gender=True):
...: gender_text = '男'
...: if not gender:
...: gender_text = '女'
...: print(f'姓名:{name} ,性别:{gender_text},年龄:{age},班级:{s_class}。')
...:
In [27]: test_print(name='wzw',age=12,s_class=6)
姓名:wzw ,性别:男,年龄:12,班级:6。
In [28]: test_print(name='wcf')
姓名:wcf ,性别:男,年龄:未知,班级:未知。
In [30]: test_print(name='xxx',gender=False)
姓名:xxx ,性别:女,年龄:未知,班级:未知。
###################################################测试_END###################################################
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
15.2 高阶函数
1)函数2调用函数1:
In [1]: def func_1(a,b):
...: return a + b
...:
In [2]: def func(a,b,func_1):
...: return(func_1(a,b) * func_1(a,b))
...:
In [3]: func(1,2,func_1)
Out[3]: 9
###注意使用内置高阶函数时,函数的参数代入的函数fx,不需要使用fx(a,b)或fx()
2)map(函数,可迭代对象) 作用:将函数一次作用到每个元素上,并将结果以可迭代对象返回
In [15]: def fx(x):
...: return x * x
...:
...: l = [1,2,3,4,5,6,7,8,9]
...:
...: res = map(fx,l)
In [16]:
In [16]: list(res)
Out[16]: [1, 4, 9, 16, 25, 36, 49, 64, 81]
3)上述map函数的实现逻辑,用for循环来简单理解:
In [17]: def fx(x):
...: return x * x
...:
In [18]: l = [1,2,3,4,5,6,7,8,9]
In [19]: res = []
In [20]: for i in l:
...: res.append(fx(i))
...:
In [21]: list(res)
Out[21]: [1, 4, 9, 16, 25, 36, 49, 64, 81]
4)reduce函数,需导入内置函数
#reduce(函数,可迭代对象) 将可迭代对象,代入函数计算结果;将结果与可迭代对象下一个元素,继续代入函数计算
In [32]: from functools import reduce
In [33]: def fx(a,b):
...: return a + b
...:
In [34]: l = [1,2,3,4,5,6,7,8,9]
In [35]: res = reduce(fx,l)
In [36]: res
Out[36]: 45
5)数字char -> int
#原理,比如'12345',(((1*10+2)*10+3)*10+4)*10+5
In [6]: def str2int(s):
...: def f(a,b):
...: return a*10 + b
...: def char2int(c):
...: return int(c)
############################这里利用map函数,将数字字符串s(可迭代对象),拆分为list(map(char2int,s)) = [1,2,3,4,5]
...: num = reduce(f,map(char2int,s))
...: return num
...:
In [7]: num = str2int('12345')
In [8]: num
Out[8]: 12345
In [9]: type(num)
Out[9]: int
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
15.3 匿名函数
In [7]: lambda x : x * x
Out[7]: <function __main__.<lambda>(x)>
In [8]: (lambda x: x * x)(3)
Out[8]: 9
#等价
In [5]: def func(x):
...: return x * x
...:
In [6]: func(3)
Out[6]: 9
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
15.4 偏函数
###类似“缺省参数”功能,但由于有些函数并不是自己定义(编写、使用)的函数,所以可以为函数增加定义属性
In [12]: from functools import partial
In [13]: int_2 = partial(int,base=2)
In [14]: int_2('100')
Out[14]: 4
In [15]: int_2('110')
Out[15]: 6
附:
int('100', base=10) # 十进制,结果为 100
int('100', base=2) # 二进制,结果为 4
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
15.5 递归函数
1)基础函数:阶乘
In [6]: def func(a):
...: if a == 1:
...: return 1
...:
...: res = (a * func(a-1))
...: return res
...:
In [7]: func(1)
Out[7]: 1
In [8]: func(5)
Out[8]: 120
2)尾递归:将结果先计算出来,防止堆栈
In [8]: def func_1(next_num,summ_num):
...: if next_num == 1:
...: return summ_num
...: return func_1(next_num - 1, next_num * summ_num)
...:
In [9]: func(3)
Out[9]: 6
In [10]:
In [10]: func(5)
Out[10]: 120
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/w854090472/python_study.git
git@gitee.com:w854090472/python_study.git
w854090472
python_study
python_study
master

搜索帮助