Python——函数
来源:互联网 发布:福昕阅读器有mac版吗 编辑:程序博客网 时间:2024/06/04 18:37
定义
def my_abs(x): if x >= 0: return x else: return -x
注意:如果没有return 语句,执行完成之后,返回return None;
返回多个值
import mathdef move(x, y, step, angle): nx = x + step * math.cos(angle) ny = y - step * math.sin(angle) return nx, nyx, y = move(100, 100, 60, math.pi / 6)print x , y
看起来是两个返回值,其实还是一个:
>>> r = move(100, 100, 60, math.pi / 6)>>> print r>>>(151.96152422706632, 70.0)
其实多个返回值,实际上是返回一个tuple
默认参数
def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x return sprint power(2) #4
注意:默认参数只能定义在必需参数的后面
可变参数
Python解释器会把传入的一组参数组装成一个tuple传递给可变参数,因此,在函数内部,直接把变量 args 看成一个 tuple 就好了:
def average(*args): if args : return sum(args)*1.0/len(args) else : return 0.0print average()print average(1, 2)print average(1, 2, 2, 3, 4)
0 0
- python——函数
- Python——函数
- Python—定义函数
- Python函数—基础
- Python函数—参数
- Python——strip()函数,title()函数
- python—数学函数/随机数函数/三角函数
- Python内建函数—isinstance()函数
- 关于python——函数
- python基础知识——函数
- Python——makefile函数
- Python——函数基础
- python学习——函数
- Python——5函数
- python基础——函数
- Python入门——函数
- Python——调用函数
- Python——fifter函数
- Sql或者hql 查询时 需要多条件时可以使用嵌套查询
- 设计模式的6大准则
- jQuery清除数组中的空值
- 数据存储接口测试用例
- 验证码倒计时
- Python——函数
- 报错 There is no Action mapped for namespace / and action name upload/.
- Codeforces 797D Broken BST dfs+模拟
- WAV文件格式
- Python常见正则表达式
- pycharm 2016.2注册码
- Android 之旅:Google 发布 Android 性能优化典范
- PM,RD,FE,UE,UI,QA,OP,DBA,BRD,MRD, PRD,FSD等缩写的全称解析
- java构造者模式