python篇3_函数
来源:互联网 发布:dw软件怎么使用 编辑:程序博客网 时间:2024/06/05 15:30
1.写法:
def funcName(param):【def关键字+函数名+括号参数+冒号】
2. 几种不同的参数的定义和使用
a. 关键字参数:比如我定义了函数say(name,word),在调用此函数的时候,say(word = 'hello',name = 'Damon'),这样准确的对应实参和形参,这样可以避免在参数较多的时候将参数混淆,参数顺序也不怕颠倒。
b. 默认参数:在定义函数的时候,指定参数的默认值:say(name = 'Damon',word = 'hello'),区别关键字参数
c. 收集参数:在定义函数的时候,不知道函数具体有几个参数:def myFunc(*params);在形参前面加上一个*,形参变成一个可以包含多个不同元素的元组,通过对元组的访问获取具体的参数;
3.函数生命周期
a. global关键字:对于全局变量,python中如果想要更改全局变量param,需要对本地的变量使用关键字global进行声明【因为此时本地变量和全局变量名字相同】:global param,然后在来更改;ps:访问全局变量不需要声明
b 函数嵌套:在函数里面再重新定义函数,被定义的函数只能在父函数里面被调用,这个是java没有的,要区别和函数调用的不同
c. lambda表达式
i. 写法:关键字 + 形参+冒号+返回值 eg:lambda x : x % 2 【对传入的参数模2取余】
ii.适用场合:一些适用次数较少的函数,建议使用lambda表达式
iii.可以对lambda表达式赋值 eg: func_add = lambda x : x+1
4.两个重要内置函数介绍:
filter()
filter(...)filter(function or None, sequence) -> list, tuple, or string【返回值】
Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
举例:filter(lambda x : x % 2,range(10)) ,【过滤出0到9之间的奇数】
理解:把sequence里面的item通过function运算,返回结果为True的sequence中的item,即true是返回依据
map()
map(function,sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
举例:map(lambda x : x*x,range(10))【对0到9之间的数字进行自乘】
理解:sequence作为集合A,函数关系是function,返回集合B
5.递归和迭代
a.递归:递归就是函数自己调用自己实现循环,要有返回条件,不然就死循环;采用的分治,把问题划分到最小单元,最小单元触发返回条件,再逐层返回到顶层,典型的递归是二分查找法;
b.迭代:迭代就是函数一段代码被反复执行实现循环,特点是循环中参与运算的变量也是保存结果的变量
- python篇3_函数
- Python 内建函数_基础篇
- 《Head First Programming》---python 3_函数
- python笔记_函数
- Python笔记_函数
- Python基础_函数
- python进阶三_函数
- python 记_匿名函数
- Python学习笔记_函数
- python成长之路第三篇(3)_内置函数及生成器迭代器 [面试]
- 3 Python 函数传参_(不)可变类型
- Python练习题(2)_函数练习
- 7.Python进阶_函数对象
- Python入门笔记(6)_ 函数
- Guibs 的 Python学习_ 函数
- Python 进阶_函数式编程
- Python内置函数_数学运算类
- Python内置函数_集合操作类
- Java 之 HTTP请求乱码解决,GZIP 返回值乱码解决
- nginx 核心应用和配置
- node mongoose
- 文件夹中图像生成txt文本 img2txt
- SIFT特征表达
- python篇3_函数
- unity3d LuaFramework ShareSDK
- cocos2d lua调用java LuaJavaBridge
- 爬虫实战2—动态网页的爬取
- 微信小程序API~GET
- Hadoop中MapReduce中的counter的意义
- linux下添加用户并赋予root权限
- jenkins部署安卓自动打包报错
- C++赋值语句讲解